SPRINGBOOT之文件上传与下载
来源:互联网 发布:易语言数据库 编辑:程序博客网 时间:2024/05/01 07:42
注意:代码的编写是在建立Marven工程的基础上编写的
比较简单,直接上代码:
单文件:
@RequestMapping(value = "/testUploadFile", method = RequestMethod.POST)
public void testUploadFile(HttpServletRequest req,
MultipartHttpServletRequest multiReq) {
// 获取上传文件的路径
String uploadFilePath = multiReq.getFile("file1").getOriginalFilename();
System.out.println("uploadFlePath:" + uploadFilePath);
// 截取上传文件的文件名
String uploadFileName = uploadFilePath.substring(
uploadFilePath.lastIndexOf('\\') + 1, uploadFilePath.indexOf('.'));
System.out.println("multiReq.getFile()" + uploadFileName);
// 截取上传文件的后缀
String uploadFileSuffix = uploadFilePath.substring(
uploadFilePath.indexOf('.') + 1, uploadFilePath.length());
System.out.println("uploadFileSuffix:" + uploadFileSuffix);
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fis = (FileInputStream) multiReq.getFile("file1").getInputStream();
fos = new FileOutputStream(new File(".//uploadFiles//" + uploadFileName
+ ".")
+ uploadFileSuffix);
byte[] temp = new byte[1024];
int i = fis.read(temp);
while (i != -1){
fos.write(temp,0,temp.length);
fos.flush();
i = fis.read(temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
多文件
@RequestMapping(value = "testUploadFiles", method = RequestMethod.POST) @ResponseBody public void handleFileUpload(HttpServletRequest request) { List<MultipartFile> files = ((MultipartHttpServletRequest) request) .getFiles("file"); MultipartFile file = null; BufferedOutputStream stream = null; for (int i = 0; i < files.size(); ++i) { file = files.get(i); if (!file.isEmpty()) { try { String uploadFilePath = file.getOriginalFilename(); System.out.println("uploadFlePath:" + uploadFilePath); // 截取上传文件的文件名 String uploadFileName = uploadFilePath .substring(uploadFilePath.lastIndexOf('\\') + 1, uploadFilePath.indexOf('.')); System.out.println("multiReq.getFile()" + uploadFileName); // 截取上传文件的后缀 String uploadFileSuffix = uploadFilePath.substring( uploadFilePath.indexOf('.') + 1, uploadFilePath.length()); System.out.println("uploadFileSuffix:" + uploadFileSuffix); stream = new BufferedOutputStream(new FileOutputStream(new File( ".//uploadFiles//" + uploadFileName + "." + uploadFileSuffix))); byte[] bytes = file.getBytes(); stream.write(bytes,0,bytes.length); } catch (Exception e) { e.printStackTrace(); } finally { try { if (stream != null) { stream.close(); } } catch (IOException e) { e.printStackTrace(); } } } else { System.out.println("上传文件为空"); } } System.out.println("文件接受成功了"); }
public void testDownload(HttpServletResponse res) { String fileName = "upload.jpg"; res.setHeader("content-type", "application/octet-stream"); res.setContentType("application/octet-stream"); res.setHeader("Content-Disposition", "attachment;filename=" + fileName); byte[] buff = new byte[1024]; BufferedInputStream bis = null; OutputStream os = null; try { os = res.getOutputStream(); bis = new BufferedInputStream(new FileInputStream(new File("d://" + fileName))); int i = bis.read(buff); while (i != -1) { os.write(buff, 0, buff.length); os.flush(); i = bis.read(buff); } } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("success"); }}
浏览器代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="http://localhost:8080/testUploadFile" method="POST" enctype="multipart/form-data"> <p>单文件上传:</><br/> <input type="file" name="file1"/> <input type="submit" value = "上传"/></form><form method="POST" enctype="multipart/form-data" action="http://localhost:8080/testUploadFiles"> <p>多文件上传:</> <p>文件1:<input type="file" name="file" /></p> <p>文件2:<input type="file" name="file" /></p> <p><input type="submit" value="上传" /></p></form><a href="http://localhost:8080/testDownload">下载</a></body></html>
- SPRINGBOOT之文件上传与下载
- SpringBoot之文件上传
- 文件上传与下载之文件下载
- springboot+文件上传和下载
- SpringBoot文件上传和下载
- SpringBoot之文件下载
- Struts2之实现文件上传与下载
- Struts2之实现文件上传与下载
- Struts2之实现文件上传与下载
- php编程之文件上传与下载
- 七、Struts2之文件上传与下载
- Struts2之文件上传与下载
- struts2.3.15之文件上传与下载
- Struts2之文件上传与下载
- Spring MVC之文件上传与下载
- Struts2之文件上传与下载
- JavaWeb之文件上传与下载详解
- SpringMVC之文件上传与下载
- 前端学习-CSS-03
- 程序猿谈恋爱什么的·QT自定义符文旋转按钮
- 组装计算机基本知识
- 09.ifndef的作用
- 使用循环CAS实现原子操作
- SPRINGBOOT之文件上传与下载
- sphinx出现WARNING: rotating index 'delta': cur to old rename failed问题
- Java中static静态小记
- HDU 2035-人见人爱A^B
- Matlab的GUI程序转换为单独可执行的exe文件
- POJ3468 线段树 区间更新
- 11.函数调用方式
- final关键字、抽象类、接口
- Android studio实现Serializable自动生成serialVersionUID