SpringMVC路径配置

我们希望文件要上传到的路径是可配置,这样我们就可以方便更改文件所放置的地方。这一节里,我们主要就是来解决这个问题.首先,我们需要在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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值