需要网站的favicon保存到数据库中。思路很简单连接internet,然后得到图片的bytes保存到数据库的blob字段就行。但是我却在取到图片的bytes上面浪费了许多时间。
开始的代码如下
boolean fetched = false;
URL url = null;
try {
url = new URL("http://" + channel.getSite().getHost() + "/favicon.ico");
HttpURLConnection httpURL = (HttpURLConnection) url.openConnection();
//httpURL.setConnectTimeout(60*1000);
httpURL.connect();
if (httpURL.getResponseCode()==200){
InputStream is = httpURL.getInputStream();
int length = is.available();
byte[] bytes = new byte[length];
is.read(bytes, 0, length);
Blob blob = Hibernate.createBlob(bytes);
((Image)image).setLocalSmallImage(blob);
logger.debug("Image URL:[" + url + "] has been fetched.");
logger.debug("read image size:" + length);
fetched = true;
}
} catch (Exception e) {
//ignored
}
if (!fetched)
logger.debug("Fail to fetch Image URL:[" + url + "] ");
在本地上设置断点调试发现一切正常。 但是发不到服务器上却发现数据更本没有保存到进去,或者只保存进去一部分。奇怪!然后log了bytes的长度(logger.debug("read image size:" + length);)。发现这时候的长度为0。上面的代码肯定是有问题。 因为网络延时之类的这里的available根本就不准确。其实这里我们应该像读取一个文件,然后在把他写入另一个文件的操作是一样的。需要先用过inputstream read出bytes, 然后再通过outputstream把bytes输出。 但是这里我们不需要写到文件之类的datasource中。我们需要的只是bytes。我们可以方便的使用ByteArrayOutputStream来存储我们的bytes。
public static byte[] getAllBytes(InputStream in) throws Exception {
int chunkSize = 4096;
byte[] b = new byte[chunkSize];
int borb = -1;
ByteArrayOutputStream fos = new ByteArrayOutputStream();
while ((borb = in.read(b)) != -1) {
fos.write(b, 0, borb);
}
return fos.toByteArray();
}
分享到:
相关推荐
System.Drawing.Image.FromStream 出现参数无效的解决方法
CompressionStream.CopyFrom(InputStream, InputStream.Size); CompressionStream.Free; OutputStream.Free; InputStream.Free;// decompress InputFileName := ‘c:image.png.bzip‘; OutputFilename := ‘c:image...
// the stream from where you read your input data InputStream inputStream = ...; // the stream to where you send commands OutputStream outputStream = ...; Expect expect = new...
java.io.Reader 和 java.io.InputStream 的区别
深入剖析tomcat第三章SocketInputStream源码 * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/StringManager.java,v 1.2 2001/07/22 20:25:14 pier Exp $ * $Revision: 1.2...
InputStream inputStream = CommonTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml"); SQLSessionFactory SQLSessionFactory = new SQLSessionFactoryBuilder().build(inputStream)...
NULL 博文链接:https://superherosk123.iteye.com/blog/611933
// send from inputStream InputStream in = ... resp = Requests.post(url).data(in).text(); // multipart 请求, 用于文件上传: Response resp = Requests.post(url).data(map).multiPart("ufile",...
Java中InputStream类.pdf 学习资料 复习资料 教学资源
Class clazz = Class.forName(userDaoClass); userDao = (UserDao) clazz.newInstance(); } catch (Throwable e) { throw new ExceptionInInitializerError(e); } } public static DaoFactory ...
Java and the Internet .......... 33 What is the Web? ......................... 33 Client-side programming ............ 34 Server-side programming ............ 38 Summary .................................
InputStream imagein2=new FileInputStream("E:/照片/摇头娃娃1.gif"); BufferedImage image=ImageIO.read(imagein); BufferedImage image2=ImageIO.read(imagein2); Graphics g=image.getGraphics(); g....
org.eclipse.swt.browser.InputStream.class org.eclipse.swt.browser.LocationAdapter.class org.eclipse.swt.browser.LocationEvent.class org.eclipse.swt.browser.LocationListener.class org.eclipse.swt....
String inputStream = null; inputStream = in.readLine(); while(inputStream!=null){ System.out.println(inputStream); inputStream = in.readLine(); } } catch (MalformedURLException e) { /...
for(String str : fileList.keySet()) if((fileList.get(str)).getFileSize() > 0 && (fileList.get(str)).getFileSize() ) { String fileName = DIRECTORY +"/"+ fileList.get(str).getFileName(); ...
刚开始简单破解了一下。 生成PDF都没问题,后来发现生成... InputStream is = XXX.class.getResourceAsStream("/pdflicense.xml"); license.setLicense(is); is.close(); 其中pdflicense.xml已经包含在jar文件里面
import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; ...
原理讲解-ServletInputStream.readLine(byte[] b, int off, int len) 方法原理讲解-ServletInputStream.readLine(byte[] b, int off, int len) 方法
Java inputstream outputstream ..... thread runnable..... 内容是中文的
InputStream is = DruidJDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); prop.load(is); ds = DruidDataSourceFactory.createDataSource(prop); } catch (IOException e) { e....