java下载网络文件
下面这段代码是下载一个http网络文件的代码,但有时候下载下来的图片是完整的,有时候下载下来的不完整,还有下载的ppt,pdf之类,也是打不开的。请大件们给指导一下,小弟感激不尽。
public String goDownLoad() throws IOException, URISyntaxException{java下载 下载网络文件 下载 下载http文件
String urlPath ="http://www.yixue360.net/images/news/pic02.png";
URL _URL=new URL(urlPath);
HttpURLConnection con=(HttpURLConnection) _URL.openConnection();
con.connect();
InputStream fis=con.getInputStream();
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
//获取文件名
String trueurl=con.getURL().toString();
String filename=trueurl.substring(trueurl.lastIndexOf('/')+1);
this.getResponse().reset();
this.getResponse().setHeader("Content-Type", "application/octet-stream");
this.getResponse().addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes(), "UTF-8"));
this.getResponse().addHeader("Content-Length", "" + con.getContentLength()+10024);
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.getResponse().addHeader("Date", formate.format(new Date()));
OutputStream toClient = this.getResponse().getOutputStream();
toClient.write(buffer);
toClient.flush();
toClient.close();
return null;
}
public static boolean fromIputStreamToFile(InputStream is,
String outfilepath) {
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(is);
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(
new FileOutputStream(outfilepath));
// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} catch (Exception e) {
return false;
} finally {
try {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
public class URLHelper {
public static String getStringByUrl(String urlstr){
String result="";;
try {
InputStream is = getInputStreamByUrl(urlstr);
result = IOHelper.fromIputStreamToString(is);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static InputStream getInputStreamByUrl(String urlstr){
urlstr=urlstr.startsWith("http://")?urlstr:"http://"+urlstr;
System.out.println(urlstr);
URL url;
URLConnection conn;
InputStream is = null;
try {
url = new URL(urlstr);
conn = url.openConnection();
is=conn.getInputStream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
}
那么 byte[] buffer = new byte[fis.available()];
这个fis.available()也就不是你想要的全部文件的长度了
[解决办法]
byte[] buffer = new byte[fis.available()];
遇到好多次人问这个问题了。上面那句代码有问题。fis.available()不是所有的文件大小
[解决办法]
因为还没有读完。
[解决办法]
public String goDownLoad() throws IOException, URISyntaxException{
String urlPath ="http://www.yixue360.net/images/news/pic02.png";
URL _URL=new URL(urlPath);
HttpURLConnection con=(HttpURLConnection) _URL.openConnection();
con.connect();
InputStream fis=con.getInputStream();
byte[] buffer = new byte[1024];//这里开一个字节数组用来每次读取装入
//获取文件名
String trueurl=con.getURL().toString();
String filename=trueurl.substring(trueurl.lastIndexOf('/')+1);
this.getResponse().reset();
this.getResponse().setHeader("Content-Type", "application/octet-stream");
this.getResponse().addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes(), "UTF-8"));
this.getResponse().addHeader("Content-Length", ""+con.getContentLength());
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.getResponse().addHeader("Date", formate.format(new Date()));
OutputStream toClient = this.getResponse().getOutputStream();
int i=-1;
while((i=fis.read(buffer))!=-1){//在这里循环读流直到末尾保证读全
toClient.write(buffer,0,i);
}
toClient.flush();
toClient.close();
return null;
}
楼主根本就没读完,对于下载网络文件这种的不要一次性就以Content-Length的长度作为整个数组的大小读一次这样有可能不能读全。
public String goDownLoad() throws IOException, URISyntaxException{
String urlPath ="http://www.yixue360.net/images/news/pic02.png";
URL _URL=new URL(urlPath);
HttpURLConnection con=(HttpURLConnection) _URL.openConnection();
con.connect();
InputStream fis=con.getInputStream();
byte[] buffer = new byte[1024];//这里开一个字节数组用来每次读取装入
//获取文件名
String trueurl=con.getURL().toString();
String filename=trueurl.substring(trueurl.lastIndexOf('/')+1);
this.getResponse().reset();
this.getResponse().setHeader("Content-Type", "application/octet-stream");
this.getResponse().addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes(), "UTF-8"));
this.getResponse().addHeader("Content-Length", ""+con.getContentLength());
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.getResponse().addHeader("Date", formate.format(new Date()));
OutputStream toClient = this.getResponse().getOutputStream();
int i=-1;
while((i=fis.read(buffer))!=-1){//在这里循环读流直到末尾保证读全
toClient.write(buffer,0,i);
}
toClient.flush();
toClient.close();
return null;
}