本篇文章主要寫下自己在使用Spring Boot搭建后臺(tái)項(xiàng)目時(shí)上傳文件遇到的一些問題吓揪,希望對(duì)你有所幫助滥朱。
問題一: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í)候仔沿,最開始也配置multipartResolver的bean,但是在文件上傳的時(shí)候尺棋,總是得到file為null,后來查詢了下官方文檔的資料绵跷,才知道原來Spring Boot的web工程中已經(jīng)內(nèi)置了一個(gè)multipartResolver的bean膘螟,如下:
原因是Spring框架先調(diào)用了系統(tǒng)內(nèi)置的MultipartResolver來處理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)源碼如下所示:
問題二:獲取Path路徑
在Spring MVC中,我們獲取文件夾的真實(shí)路徑是通過getRealPath這個(gè)方法俘闯,如下:
// 上傳路徑
String path = session.getServletContext().getRealPath("/upload/");
但是在Spring Boot中潭苞,從app端發(fā)送上傳文件請(qǐng)求,獲取到的路徑是這樣的:
寫單元測試發(fā)送網(wǎng)絡(luò)請(qǐng)求獲取到的路徑是這樣的:
這說明getRealPath方法并不是很通用真朗,只適用于部分的情況此疹。推薦使用java.nio.file.Paths這個(gè)類來獲取文件夾路徑,比如:
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;
}
}
問題三:如何用單元測試測試文件上傳
單元測試測試文件上傳遮婶,主要是用到了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();
}
}
}
這樣就完成了上傳文件的測試旗扑,代碼還是比較簡單的蹦骑。