springmvc文件上传和文件下载
2017-05-11 11:18
375 查看
1.首先springMVC的配置文件:
导需要的入包
2.前端页面
<form name="form4" action="/fileupload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="upload"/>
</form>
3.后端接收代码:
/**
* 上传文件
*/
@RequestMapping(value = "/fileupload",method = RequestMethod.POST)
public ModelAndView fileUpload(@RequestParam(value = "file") MultipartFile multipartFile) throws IOException {
String tempPath = "d://temp" + File.separator;
File dir = new File(tempPath);
if (!dir.exists()) {
dir.mkdirs();
}
String storageFilePath = null;//文件路径
String originalFilename = null;//原文件
if (multipartFile.isEmpty()) {
throw new RuntimeException("文件不存在");
} else {
originalFilename = multipartFile.getOriginalFilename();
if (null != originalFilename && originalFilename.length() > 0) {
storageFilePath = tempPath + originalFilename;
try {
Files.copy(multipartFile.getInputStream(), Paths.get(storageFilePath));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("文件上传失败,请重试!!");
}
}
}
return new ModelAndView("success");
}4.后端下载代码:
/**
* 文件下载
* @param response
* @throws Exception
*/
@RequestMapping(value = "/downFile")
public void downFile(HttpServletResponse response)throws Exception{
String pdfPath = "d:"+File.separator+ "temp" + File.separator + "aa.pdf";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=aa.pdf");
ServletOutputStream output = response.getOutputStream();
BufferedOutputStream bufferOut = new BufferedOutputStream(output);
InputStream inputStream = new FileInputStream(pdfPath);
byte[] buffer = new byte[5 * 1024];
int length = 0;
while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
bufferOut.write(buffer, 0, length);
}
bufferOut.flush();
bufferOut.close();
output.close();
inputStream.close();
}
<!-- 配置 MultipartResolver 文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="1024000000"/> </bean> <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException --> <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 --> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop> </props> </property> </bean>
导需要的入包
<!-- commons包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
2.前端页面
<form name="form4" action="/fileupload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="upload"/>
</form>
3.后端接收代码:
/**
* 上传文件
*/
@RequestMapping(value = "/fileupload",method = RequestMethod.POST)
public ModelAndView fileUpload(@RequestParam(value = "file") MultipartFile multipartFile) throws IOException {
String tempPath = "d://temp" + File.separator;
File dir = new File(tempPath);
if (!dir.exists()) {
dir.mkdirs();
}
String storageFilePath = null;//文件路径
String originalFilename = null;//原文件
if (multipartFile.isEmpty()) {
throw new RuntimeException("文件不存在");
} else {
originalFilename = multipartFile.getOriginalFilename();
if (null != originalFilename && originalFilename.length() > 0) {
storageFilePath = tempPath + originalFilename;
try {
Files.copy(multipartFile.getInputStream(), Paths.get(storageFilePath));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("文件上传失败,请重试!!");
}
}
}
return new ModelAndView("success");
}4.后端下载代码:
/**
* 文件下载
* @param response
* @throws Exception
*/
@RequestMapping(value = "/downFile")
public void downFile(HttpServletResponse response)throws Exception{
String pdfPath = "d:"+File.separator+ "temp" + File.separator + "aa.pdf";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=aa.pdf");
ServletOutputStream output = response.getOutputStream();
BufferedOutputStream bufferOut = new BufferedOutputStream(output);
InputStream inputStream = new FileInputStream(pdfPath);
byte[] buffer = new byte[5 * 1024];
int length = 0;
while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
bufferOut.write(buffer, 0, length);
}
bufferOut.flush();
bufferOut.close();
output.close();
inputStream.close();
}
相关文章推荐
- springMVC文件上传和下载
- springMVC文件上传下载-实例2
- springMVC中文件的下载和上传
- SpringMVC下Excel文件的上传下载实现
- SpringMVC文件的上传与下载
- springmvc和servlet下的文件上传和下载(存文件目录和存数据库Blob两种方式)
- myBatis + SpringMVC上传、下载文件
- springMVC文件上传下载
- SpringMVC文件下载与上传
- SpringMVC——返回JSON数据&&文件上传下载
- myBatis + SpringMVC上传、下载文件
- springmvc和servlet在上传和下载文件(保持文件夹和存储数据库Blob两种方式)
- springMvc 中 Excel批量数据上传,利用POI解析数据保存入数据库 及模板文件下载
- SpringMVC文件上传和下载
- SpringMVC中文件上传下载
- springMVC文件上传下载-实例1
- SpringMVC配置,简单实例,文件上传与下载,ajax请求
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载
- SpringMvc 上传/下载 文件