访问服务器地址中的某个文件链接,下载文件到客户端
2016-06-28 20:52
495 查看
public void download() {
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null;
HttpServletRequest request = ThreadContextHolder.getHttpRequest();
HttpServletResponse response = ThreadContextHolder.getHttpResponse();
Integer yml_id = Integer.valueOf(request.getParameter("yml_id"));
YmlModel yml = this.logiManager.queryforYml(yml_id);
String file = null;
String fileUrl="";
String savePath = request.getRealPath("/")+"test/upyml/";
if(yml!=null){
file = yml.getYml_upfile();
}
String fileName=file;
fileUrl=savePath+file;
try {
fileUrl = new String(fileUrl.getBytes("utf-8"), "utf-8");
response.setContentType("application/x-msdownload");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
// 通知客户文件的MIME类型:
bis = new java.io.BufferedInputStream(new java.io.FileInputStream(
(fileUrl)));
bos = new java.io.BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
int i = 0;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
i++;
}
bos.flush();
this.showSuccessJson("下载成功!");
} catch (Exception e) {
e.printStackTrace();
this.showSuccessJson("下载失败!");
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bis = null;
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bos = null;
}
}
}
其中fileUrl为服务器中的文件真实路径,fileName为文件名。
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null;
HttpServletRequest request = ThreadContextHolder.getHttpRequest();
HttpServletResponse response = ThreadContextHolder.getHttpResponse();
Integer yml_id = Integer.valueOf(request.getParameter("yml_id"));
YmlModel yml = this.logiManager.queryforYml(yml_id);
String file = null;
String fileUrl="";
String savePath = request.getRealPath("/")+"test/upyml/";
if(yml!=null){
file = yml.getYml_upfile();
}
String fileName=file;
fileUrl=savePath+file;
try {
fileUrl = new String(fileUrl.getBytes("utf-8"), "utf-8");
response.setContentType("application/x-msdownload");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
// 通知客户文件的MIME类型:
bis = new java.io.BufferedInputStream(new java.io.FileInputStream(
(fileUrl)));
bos = new java.io.BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
int i = 0;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
i++;
}
bos.flush();
this.showSuccessJson("下载成功!");
} catch (Exception e) {
e.printStackTrace();
this.showSuccessJson("下载失败!");
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bis = null;
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bos = null;
}
}
}
其中fileUrl为服务器中的文件真实路径,fileName为文件名。
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树