版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hlw521hxq/article/details/83448484
上传
注意事项:表单提交方式是post,必须有属性enctype=“multipart/form-data”
spring.xml文件中,添加
<!--上传下载,通用部件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" p:maxUploadSize="5242880">
</bean>
<!--设置静态资源的访问-->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources mapping="/img/**" location="/img/"></mvc:resources>
3.获取服务器的路径的java代码
String realPath = request.getServletContext().getRealPath("/");
4.方法参数设置
(HttpServletRequest request(为了获得项目部署路径),MultipartFile img(文件参数),表单其他参数)
5.获取下载文件名的方法
img.getOriginalFilename()
下载
注意点:
-
请求是get请求,路径拼接文件下载名
-
controller返回类型是RequestEntry<>
-
关于ResponseEntry的构造方法参数(文件流形式,头信息,响应码)
文件流形式是通过FileUtils.readFileToByteArray(f)
-
设置头信息
-
响应码
-
需要获得图像的对象