您的位置:首页 > 编程语言 > Java开发

java 下载文件功能

2016-03-09 00:00 148 查看
public String downLoadFileContent() throws IOException{
// 下载网络文件
int bytesum = 0;
int byteread = 0;
response = CustomerUtil.getResponse();
request = CustomerUtil.getRequest();
String filePath = request.getParameter("filePath");//http://localhost:8080/mypro/img
String fileName = request.getParameter("fileName");//201212121212.txt
URL url = new URL(filePath+fileName);

try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
OutputStream fs=response.getOutputStream();
//设置response的编码方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("gbk"),"iso-8859-1"));
response.setHeader("Content-Disposition","attachment;filename="+fileName);
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


本文出自 “小浩” 博客,请务必保留此出处http://zhangchi.blog.51cto.com/5214280/1392282
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 下载