我们希望文件要上传到的路径是可配置,这样我们就可以方便更改文件所放置的地方。这一节里,我们主要就是来解决这个问题.首先,我们需要在config的包下创建PicturesUploadProperties类。

 package masterSpringMvc.config;
import org.springframework.boot.context.properties.
ConfigurationProperties;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import java.io.IOException;
@ConfigurationProperties(prefix = "upload.pictures")
public class PictureUploadProperties {
private Resource uploadPath;
private Resource anonymousPicture;
public Resource getAnonymousPicture() {
return anonymousPicture;
}
public void setAnonymousPicture(String anonymousPicture) {
this.anonymousPicture = new DefaultResourceLoader().
getResource(anonymousPicture);
}
public Resource getUploadPath() {
return uploadPath;
}
public void setUploadPath(String uploadPath) {
this.uploadPath = new DefaultResourceLoader().
getResource(uploadPath);
}
}

其次,在类PicturesUploadProperties上添加注解

@SpringBootApplication
@EnableConfigurationProperties({PictureUploadProperties.class})
public class MasterSpringMvc4Application extends
WebMvcConfigurerAdapter {
// code omitted
}

再次,我们要在application.properties的文件上添加下面的值

upload.pictures.uploadPath=file:./pictures
upload.pictures.anonymousPicture=classpath:/images/anonymous.png

最后, 我们修改PictureUploadController类的方法。

package masterSpringMvc.profile;
import masterSpringMvc.config.PictureUploadProperties;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLConnection;
@Controller
public class PictureUploadController {
private final Resource picturesDir;
private final Resource anonymousPicture;
@Autowired
public PictureUploadController(PictureUploadProperties
uploadProperties) {
picturesDir = uploadProperties.getUploadPath();
anonymousPicture = uploadProperties.getAnonymousPicture();
}
@RequestMapping(value = "/uploadedPicture")
public void getUploadedPicture(HttpServletResponse response)
throws IOException {
response.setHeader("Content-Type", URLConnection.guessContentT
ypeFromName(anonymousPicture.getFilename()));
IOUtils.copy(anonymousPicture.getInputStream(), response.
getOutputStream());
}
private Resource copyFileToPictures(MultipartFile file) throws
IOException {
String fileExtension = getFileExtension(file.
getOriginalFilename());
File tempFile = File.createTempFile("pic", fileExtension,
picturesDir.getFile());
try (InputStream in = file.getInputStream();
OutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
return new FileSystemResource(tempFile);
}
// The rest of the code remains the same
}

源码下载:git@github.com:owenwilliam/masterSpringMVC.git

SpringMVC路径配置相关推荐

  1. SpringMVC默认访问路径配置

    SpringMVC默认访问路径配置 需求:只访问域名,不加任何action路径,想访问默认的一个action. 比如www.jd.com,就跳到京东首页,会加载出辣么多东西来 关于这个的方法,网上有千 ...

  2. 使用注解开发SpringMVC详细配置教程

    目录 1.使用注解开发SpringMVC 1.新建一个普通的maven项目,添加web支持 2.在pom.xml中导入相关依赖 3.配置web.xml 4.编写SpringMVC配置文件 1. 自动扫 ...

  3. SpringMVC基础配置及使用

    SpringMVC基础配置及使用 SpringMVC: 1.SpringMVC和Spring的关系:     软件开发的三层架构: web层[表示层.表现层]---->Service层----& ...

  4. tns03505 无法解析名称_SpringBootWeb源码解析SpringMVC自动配置

    SpringMVC自动配置 在 Spring Boot 中引入了 spring-boot-starter-web 依赖,并完成了 DispatcherServlet 的自动配置之后,便会通过 WebM ...

  5. 框架写mysql插入为空_学习springMVC框架配置遇到的问题-数据写入不进数据库时的处理办法...

    Idea简单SpringMVC框架配置 前边已经介绍过了Struts在Idea上的配置,相对于Struts来说,我觉得SpringMVC有更多的优势,首先Struts是需要对action进行配置,页面 ...

  6. SpringBoot之SpringMVC自动配置

    关于SpringBoot中的SpringMVC自动配置的一些思考 : 自动配置 Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:(We ...

  7. 5、SpringMVC自动配置概览

    1.SpringMVC自动配置概览 Spring Boot provides auto-configuration for Spring MVC that works well with most a ...

  8. SpringMvc框架配置过滤器

    SpringMvc框架配置过滤器 本文以简单的登录过滤器为例 开发环境 jdk 8 idea2019.3.5 maven 3.6.3 spring-webmvc 5.1.9.RELEASE web.x ...

  9. nodejs安装及npm模块插件安装路径配置

    在学习完js后,我们就要进入nodejs的学习,因此就必须配置nodejs和npm的属性了. 我相信,个别人在安装时会遇到这样那样的问题,看着同学都已装好,难免会焦虑起来.于是就开始上网查找解决方案, ...

  10. 024:模版查找路径配置

    模版查找路径配置: 在项目的 settings.py 文件中.有一个 TEMPLATES 配置,这个配置包含了模板引擎的配置,模板查找路径的配置,模板上下文的配置等.模板路径可以在两个地方配置. 1. ...

最新文章

  1. bzoj 1877: [SDOI2009]晨跑 (网络流)
  2. php学历低,学历低学起php来难不难
  3. JAVA编码(20)——JAVA使用f1j9swing来生成excel文件
  4. 进程互斥的软件实现方法
  5. SpringBoot Serverless 实战 | 监控调试
  6. 为什么我会觉得SegmentFault做得越来越力不从心了?
  7. xcode 4 制作静态库(转)
  8. 自动生成相机标定轨迹
  9. ECSHOP 商品详情页相关属性商品 由新到旧排序
  10. mysql insert on update_我可以始终使用INSERT…ON DUPLICATE UPDATE进行简单的更新/插入吗?...
  11. 遇到不适当的参数_高清兽用B超机参数调整——“增益”
  12. WPS客户端更新日志留着备用
  13. python 身高预测
  14. python入门指南by许半仙百度云-《江火欲燃山》《这题超纲了》《Python入门指南》...
  15. matlab求信号的瞬时相位,phrase MATLAB中关于信号瞬时相位 频率的提取的代码,值得学习,很实用 267万源代码下载- www.pudn.com...
  16. 为什么人力资源部门一定要部署RPA
  17. BIM用C语言编程,实现BIM技术的三个重要方面是()。A.BIM的建立B.BIM的应用C.BIM的管理D.BIM的粒度E.BIM的概念...
  18. 计算机主板电池没电的状态,电脑主板电池没电引起的故障检查修理_提示CMOS电池电量不足怎么办...
  19. 四川安湖科技:抖音电商节怎么参加
  20. 听Alluxio小姐姐讲述:Alluxio云上K8S部署如何加速深度学习训练

热门文章

  1. Android Fragment-APP调用其他APP中的Fragment 1
  2. [iphone-游戏]游戏中常用的数据组织方式和解析
  3. web安全day34:一步一步学习Linux防火墙
  4. Xshell7连接VMware15虚拟机上的Ubuntu详细步骤
  5. 游戏开发之C++多继承及虚继承(C++基础)
  6. MySQL主从同步(三)——M-S-S架构配置实战
  7. 虚拟机中的linux系统无法获得ip(ifconfig命令无法查到ip)
  8. CoreData手动创建托管对象子类时报错
  9. Android aidl Binder框架浅析
  10. 简要解析红外摄像机技术与市场