根據request,上傳檔案(使用Spring CommonsMultipartResolver 上傳檔案)
阿新 • • 發佈:2018-11-11
/**
* 根據request,獲取上傳的非結構化資料
* 備註:非結構化資料會儲存臨時檔案,並返回臨時檔案路徑的集合
*
* @param request
* @return List<String>:上傳檔案的集合,其中string為檔案的臨時路徑
* @throws TranFailException
*/
public List<String> getReqFile(HttpServletRequest request) throws TranFailException {
List<String> fileList = new ArrayList<String>();
try {
// 建立一個通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
// 判斷 request 是否有檔案上傳,即多部分請求
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;// 轉換成多部分request
Iterator<String> iter = multiRequest.getFileNames();// 取得request中的所有檔名
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile(iter.next());// 取得上傳檔案
if (file != null) {
String myFileName = file.getOriginalFilename() == null ? null : file.getOriginalFilename().trim();// 取得當前上傳檔案的檔名稱
// 如果名稱不為"",說明該檔案存在,否則說明該檔案不存在
if (!"".equals(myFileName) && myFileName != null) {
myFileName = myFileName.toLowerCase();
int fileType = myFileName.lastIndexOf(".");
if (fileType == -1) {
System.out.println("檔案格式不正確,無副檔名="+ myFileName);
} else {
String extension = myFileName.substring(fileType + 1);// 副檔名
String localFilePath = "D:\\a"; // 建立檔案路徑
File dest = new File(localFilePath); // 將MultipartFile轉成file存在本地 再從本地拿到檔案進行壓縮上傳
file.transferTo(dest);
ImgUtil.compressPic(dest, localFilePath); //壓縮檔案
fileList.add(localFilePath);
}
}
}
}
}
} catch (Exception e) {
}
return fileList;
* 根據request,獲取上傳的非結構化資料
* 備註:非結構化資料會儲存臨時檔案,並返回臨時檔案路徑的集合
*
* @param request
* @return List<String>:上傳檔案的集合,其中string為檔案的臨時路徑
* @throws TranFailException
*/
public List<String> getReqFile(HttpServletRequest request) throws TranFailException {
List<String> fileList = new ArrayList<String>();
try {
// 建立一個通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
// 判斷 request 是否有檔案上傳,即多部分請求
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;// 轉換成多部分request
Iterator<String> iter = multiRequest.getFileNames();// 取得request中的所有檔名
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile(iter.next());// 取得上傳檔案
if (file != null) {
String myFileName = file.getOriginalFilename() == null ? null : file.getOriginalFilename().trim();// 取得當前上傳檔案的檔名稱
// 如果名稱不為"",說明該檔案存在,否則說明該檔案不存在
if (!"".equals(myFileName) && myFileName != null) {
myFileName = myFileName.toLowerCase();
int fileType = myFileName.lastIndexOf(".");
if (fileType == -1) {
System.out.println("檔案格式不正確,無副檔名="+ myFileName);
} else {
String extension = myFileName.substring(fileType + 1);// 副檔名
String localFilePath = "D:\\a"; // 建立檔案路徑
File dest = new File(localFilePath); // 將MultipartFile轉成file存在本地 再從本地拿到檔案進行壓縮上傳
file.transferTo(dest);
ImgUtil.compressPic(dest, localFilePath); //壓縮檔案
fileList.add(localFilePath);
}
}
}
}
}
} catch (Exception e) {
}
return fileList;
}
詳解連結:http://yanglei008.iteye.com/blog/246920
希望對你有幫助!