Spring mvc 文件下载
来源:互联网 发布:淘宝商盟北京商盟 编辑:程序博客网 时间:2024/05/17 04:34
@Controller
public class FileDownLoadController {
@RequestMapping(value="download.htm",method = RequestMethod.POST)
String filepathString = request.getParameter("filePath");
response.setContentType("multipart/form-data");
//设置下载文件的表头并对其中的中文字符进行处理(如不处理,中文名是无法显示的)
response.setHeader("Content-Disposition", "attachment;filename=" +new String( filenameString.getBytes("GBK"), "ISO8859-1"));
try {
File file=new File(filenameString);
//打印绝对路径
//System.out.println(file.getCanonicalPath());
InputStream inputStream=new FileInputStream(filepathString+"\\"+file);
OutputStream os=response.getOutputStream();
byte[] b=new byte[1024];
int length;
while((length=inputStream.read(b))>0){
os.write(b,0,length);
}
inputStream.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public class FileDownLoadController {
@RequestMapping(value="download.htm",method = RequestMethod.POST)
public @ResponseBody String FileDownLoad(HttpServletResponse response,HttpServletRequest request) throws UnsupportedEncodingException{
//这里是从前台页面获取到的文件名和文件路径(我是把这两个属性上传的时候存到了数据库里)
String filenameString = request.getParameter("fileName");String filepathString = request.getParameter("filePath");
response.setContentType("multipart/form-data");
//设置下载文件的表头并对其中的中文字符进行处理(如不处理,中文名是无法显示的)
response.setHeader("Content-Disposition", "attachment;filename=" +new String( filenameString.getBytes("GBK"), "ISO8859-1"));
try {
File file=new File(filenameString);
//打印绝对路径
//System.out.println(file.getCanonicalPath());
InputStream inputStream=new FileInputStream(filepathString+"\\"+file);
OutputStream os=response.getOutputStream();
byte[] b=new byte[1024];
int length;
while((length=inputStream.read(b))>0){
os.write(b,0,length);
}
inputStream.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
0 0
- Spring MVC 文件下载
- Spring mvc 文件下载
- spring mvc 文件下载
- Spring MVC下载文件
- spring mvc 文件下载
- Spring mvc 文件下载
- Spring MVC文件下载
- spring mvc下载文件
- Spring MVC 文件下载
- spring mvc文件下载
- Spring mvc 下载文件
- spring MVC文件下载
- Spring MVC 文件下载
- spring mvc 文件的下载
- 使用spring mvc 下载文件
- Spring MVC的文件下载
- spring mvc 4 文件下载
- spring mvc 下载excel 文件
- 监控和剖析数据库操作 -- P6Spy、SQL Profiler、IronTrack SQL 使用简介
- java.util.concurrent.locks与synchronized及其异同
- 单链表的逆置
- JavaScript学习久 jquery3
- 关于杭州电子科技大学的acm1002, 1003, 1004, 1005, 1008, 1009, 1012, 1013,1014,1015,1016,1017,1019,1020,1021,1022
- Spring mvc 文件下载
- P31 signed and unsigned
- 辛星和您一起学vim脚本第一节
- mysql及oracle中的group_contact小结
- Form pre增删改
- OAuth和OpenID的区别
- 模式识别学习初感悟
- Java RMI原理与使用---基础篇
- C语言中的初始化及内存分配