java下载网络中的文件,java下载网络文件解决思路

java下载网络文件

下面这段代码是下载一个http网络文件的代码,但有时候下载下来的图片是完整的,有时候下载下来的不完整,还有下载的ppt,pdf之类,也是打不开的。请大件们给指导一下,小弟感激不尽。

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[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;

}

java下载

下载网络文件

下载

下载http文件

分享到:

------解决方案--------------------

文件大小是不是超过了你设的Content-Length

------解决方案--------------------

用缓冲流缓冲一下。

要不容易出错。

InputStream is = URLHelper.getInputStreamByUrl("http://upload.newhua.com/6/73/1291012043416.jpg");

IOHelper.fromIputStreamToFile(is, "D:\\PP.jpg");

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;

}

}

------解决方案--------------------

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);

是你设置了大小

------解决方案--------------------

inputStream.available()的api文档是这么说的

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值