加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java Spring MVC 上传下载文件配置及controller方法详解

发布时间:2020-12-14 19:58:15 所属栏目:Java 来源:网络整理
导读:下载: 1.在spring-mvc中配置(用于100M以下的文件下载) bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" property name="messageConverters" list !--配置下载返回类型--bean class="org.springframework.http

下载:

1.在spring-mvc中配置(用于100M以下的文件下载)

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
<property name="messageConverters"> 
<list> 
<!--配置下载返回类型-->
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> 
<bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
<!--配置编码方式-->
<property name="supportedMediaTypes" value="application/json; charset=UTF-8" /> 
</bean> 
</list> 
</property> 
</bean>

下载文件代码

@RequestMapping("/file/{name.rp}")
public ResponseEntity<byte[]> fileDownLoad(@PathVariable("name.rp")String name,HttpServletRequest request,HttpServletResponse response) {
// @PathVariable String name,// @RequestParam("name")String name,// System.out.println("<name>"+name);
// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
ResponseEntity<byte[]> re = null;
try {
/**
* css,js,json,gif,png,bmp,jpg,ico,doc,docx,xls,xlsx,txt,swf,pdf
* **/
//下载防止静态加载干扰
Feelutile f=new Feelutile();
name=f.getfileformat(name);
String pathString="C:tempDirectory"+name; 
File file=new File(pathString);
HttpHeaders headers=new HttpHeaders();
//String filename=URLEncoder.encode(name,"UTF-8");//为了解决中文名称乱码问题 
String filename=new String(name.getBytes("utf-8"),"utf-8");
byte[] by=FileUtils.readFileToByteArray(file);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//URLEncoder.encode(filename,"UTF-8")
headers.setContentDispositionFormData("attachment",filename);
headers.setContentLength(by.length);
re=new ResponseEntity<byte[]>(by,headers,HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
try {
request.getRequestDispatcher("/error/404.jsp").forward(request,response);
} catch (ServletException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return re;
}

上传文件:

1在spring-mvc中配置

<!--4.文件上传 配置 file upload -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>1048576000</value>
</property>
<property name="maxInMemorySize">
<value>40960</value>
</property>
</bean>

在controller中代码如下

@RequestMapping(value="/upload",method = RequestMethod.POST)
@ResponseBody
public Json upload(Doc doc,@RequestParam("uploadFile") CommonsMultipartFile file) {
Json j = new Json();
try {
String realpath = this.servletContext.getRealPath("/upload"); 
String uploadFileFileName = file.getOriginalFilename(); 
String uploadFileFileNameWithoutSpace = uploadFileFileName.replaceAll(" ",""); 
String fileType = uploadFileFileNameWithoutSpace.substring(uploadFileFileNameWithoutSpace.lastIndexOf("."));
File targetFile = new File(realpath+File.separator,uploadFileFileNameWithoutSpace);
if (targetFile.exists()) {
targetFile.delete();
}
file.getFileItem().write(targetFile); 
docService.upload(doc,uploadFileFileNameWithoutSpace);
j.setSuccess(true);
j.setMsg("Upload manual successfully");
}catch (Exception e) {
logger.error(ExceptionUtil.getExceptionMessage(e));
j.setMsg("Upload manual unsuccessfully");
}
return j;
}

以上所述是小编给大家介绍的Java Spring MVC 上传下载文件配置及controller方法详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

您可能感兴趣的文章:

  • 详解利用SpringMVC拦截器控制Controller返回值
  • springMVC如何将controller中数据传递到jsp页面
  • spring mvc利用ajax向controller传递对象的方法示例
  • springMVC如何将controller中Model数据传递到jsp页面
  • Spring MVC 关于controller的字符编码问题
  • SpringMVC实现controller中获取session的实例代码
  • 详解springmvc 中controller与jsp传值
  • SpringMVC中controller接收json数据的方法
  • SpringMVC基于注解的Controller详解
  • Spring MVC学习笔记之Controller查找(基于Spring4.0.3)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读