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