您的位置:首页 > 理论基础 > 计算机网络

http下载文件

2015-12-14 00:00 543 查看
InputStream bis = null;
BufferedOutputStream bos = null;

try {
response.setContentType("UTF-8");
response.setCharacterEncoding("UTF-8");

//			if (uploadFile.getExtend().equals("text")) {
//				response.setContentType("text/plain;");
//			} else if (uploadFile.getExtend().equals("doc")) {
//				response.setContentType("application/msword;");
//			} else if (uploadFile.getExtend().equals("xls")) {
//				response.setContentType("application/ms-excel;");
//			} else if (uploadFile.getExtend().equals("pdf")) {
//				response.setContentType("application/pdf;");
//			} else if (uploadFile.getExtend().equals("jpg") || uploadFile.getExtend().equals("jpeg")) {
//				response.setContentType("image/jpeg;");
//			} else {
//				response.setContentType("application/x-msdownload;");
//			}
response.setContentType("text/plain;");

response.setHeader("Content-disposition", "attachment; filename=menu.json");
response.setHeader("Content-Length", ""+(new File(fileName)).length());

bos = new BufferedOutputStream(response.getOutputStream());
bis = new BufferedInputStream(new FileInputStream(fileName));
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: