本篇文章主要寫下自己在使用Spring Boot搭建后臺(tái)項(xiàng)目時(shí)上傳文件遇到的一些問(wèn)題,希望對(duì)你有所幫助。
問(wèn)題一:CommonsMultipartResolver
在使用Spring MVC文件上傳的時(shí)候,我們會(huì)在springmvc-config.xml中配置CommonsMultipartResolver,如下:
<!-- 配置Spring MVC文件上傳-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上傳文件大小上限,單位為字節(jié)(10MB) -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
<!-- 請(qǐng)求的編碼格式,必須和JSP的pageEncoding屬性一致,以便正確讀取表單的內(nèi)容,默認(rèn)為ISO-8859-1 -->
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
在使用Spring Boot的時(shí)候,最開(kāi)始也配置multipartResolver的bean,但是在文件上傳的時(shí)候,總是得到file為null,后來(lái)查詢了下官方文檔的資料,才知道原來(lái)Spring Boot的web工程中已經(jīng)內(nèi)置了一個(gè)multipartResolver的bean,如下:

原因是Spring框架先調(diào)用了系統(tǒng)內(nèi)置的MultipartResolver來(lái)處理http multi-part請(qǐng)求,這個(gè)時(shí)候http multipart的請(qǐng)求已經(jīng)被處理掉了,后面又移交給自定義的bean,自定義的bean就獲取不到相應(yīng)的http multi-part請(qǐng)求了,所以取到的file就為null了。解決辦法就是把自定義的bean給移除掉就好了。內(nèi)置的multipartResolver默認(rèn)的最大大小為10MB。內(nèi)置的multipartResolver實(shí)現(xiàn)源碼如下所示:

問(wèn)題二:獲取Path路徑
在Spring MVC中,我們獲取文件夾的真實(shí)路徑是通過(guò)getRealPath這個(gè)方法,如下:
// 上傳路徑
String path = session.getServletContext().getRealPath("/upload/");
但是在Spring Boot中,從app端發(fā)送上傳文件請(qǐng)求,獲取到的路徑是這樣的:

寫單元測(cè)試發(fā)送網(wǎng)絡(luò)請(qǐng)求獲取到的路徑是這樣的:

這說(shuō)明getRealPath方法并不是很通用,只適用于部分的情況。推薦使用java.nio.file.Paths這個(gè)類來(lái)獲取文件夾路徑,比如:
private final Path rootLocation = Paths.get("upload");
存儲(chǔ)文件的Service類實(shí)現(xiàn)如下:
@Service("saveFileService")
public class SaveFileService {
private final Path rootLocation = Paths.get("upload");
public boolean store(MultipartFile file) {
boolean isSucess = false;
try {
// 創(chuàng)建一個(gè)文件夾
Files.createDirectories(rootLocation);
Path savePath = this.rootLocation.resolve(file.getOriginalFilename()+".jpeg");
file.transferTo(new File(savePath.toAbsolutePath().toString()));
isSucess = true;
} catch (IOException e) {
isSucess = false;
}
return isSucess;
}
public boolean deleteAll() {
return FileSystemUtils.deleteRecursively(rootLocation.toFile());
}
public Resource loadFile(String fileName) {
Path path = rootLocation.resolve(fileName);
Resource resource = null;
try {
resource = new UrlResource(path.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
} else {
throw new RuntimeException("Load File Fail!");
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return resource;
}
}
問(wèn)題三:如何用單元測(cè)試測(cè)試文件上傳
單元測(cè)試測(cè)試文件上傳,主要是用到了MockMultipartFile這個(gè)類,另外還是用到了fileUpload這個(gè)類,實(shí)現(xiàn)代碼如下:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UploadFileTests {
@Autowired
private MockMvc mockMvc;
@MockBean
private DocumentService documentService;
@Before
public void setUp() {
Document document = new Document();
document.setTitle("高圓圓");
document.setRemark("大美女一枚");
User user = new User();
user.setId(1);
document.setUser(user);
document.setFileName("userAvatar.jpeg");
BDDMockito.given(documentService.addDocument(document)).willReturn(true);
}
@Test
public void testUploadTest() {
SortedMap<String, String> sortedMap = new TreeMap<>();
sortedMap.put("timeStamp", System.currentTimeMillis()+"");
sortedMap.put("ownerId", "1");
sortedMap.put("title", "劉亦菲");
sortedMap.put("remark", "古典美女");
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
stringBuilder.append(entry.getKey() + "=" + entry.getValue());
}
String sign = MD5Util.createMD5Sign(sortedMap, BootConstants.SIGN_KEY);
System.out.println("sign : " + sign);
sortedMap.put("sign", sign);
String mapStr = JSON.toJSONString(sortedMap, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
String encryptStr = AESUtil.encrypt(mapStr, BootConstants.AES_KEY, BootConstants.AES_IV);
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("hrm2.jpg");
try {
MockMultipartFile multipartFile = new MockMultipartFile("file", "userAvatar5", "image/jpeg", inputStream);
mockMvc.perform(fileUpload("/hrm/api/documents").file(multipartFile).param("Encrypt", encryptStr))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
這樣就完成了上傳文件的測(cè)試,代碼還是比較簡(jiǎn)單的。