gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Java Spring MVC 上传下载文件配置及controller方法详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

下载:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
<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>

下载文件代码 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@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中配置 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 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中代码如下 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@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方法详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。

原文链接:http://www.cnblogs.com/hongdw/archive/2016/09/30/5924115.html 。

最后此篇关于Java Spring MVC 上传下载文件配置及controller方法详解的文章就讲到这里了,如果你想了解更多关于Java Spring MVC 上传下载文件配置及controller方法详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com