/**
* 二進制文件上傳(前端)
*
* @param multipartFile MultipartFile上傳文件的二進制流
* @return fileUploadMap
* @see org.springframework.web.multipart.MultipartFile
*/
public Map<String, String> getFileUpload(MultipartFile multipartFile, String name, String uuid, String type,HttpServletResponse response) {
// MultipartResolver resolver = new CommonsMultipartResolver(httpServletRequest.getSession().getServletContext());
// MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(httpServletRequest);
// List<MultipartFile> multipartFiles = multipartRequest.getFiles("file");
Map<String, String> fileUploadMap = new HashMap<String, String>();
// for (MultipartFile multipartFile : multipartFiles) {
//獲取文件流
InputStream fileInputStream = null;
try {
fileInputStream = multipartFile.getInputStream();
if (null == fileInputStream) {
}
} catch (Exception e) {
this.setError("上傳文件:", "獲取文件流", e.getMessage());
//
}
//獲取uuid作爲新的文件名稱
String newFileName = UUID.randomUUID().toString();
String dotExtensionName = StringUtil.getDotExtensionName(multipartFile.getOriginalFilename());
//如果有以點開始的正常的文件擴展名,則將文件擴展名附加進來
//如果不存在以點開始的正常的文件擴展名,則直接使用uuid,不附加擴
//展名的方式保存文件
if (dotExtensionName != null) {
newFileName += dotExtensionName;
}
//寫文件
File newFile = FileUtil.createUploadDirectoryAndFile(newFileName);
if (newFile == null) {
this.setError("文件名:" + newFile.getPath(), "創建", "失敗");
return null;
}
this.setInfo("文件名:" + newFile.getPath(), "創建", "成功");
try {
//將來自InputSteam中的流複製到目標路徑下,如果不存在會自動創建,如果存在則會覆蓋,然後關閉流。(也有不關閉流的方法,自己查)
FileUtils.copyInputStreamToFile(fileInputStream, newFile);
this.setInfo("文件名:" + newFile.getPath(), "寫入", "成功");
} catch (IOException e) {
this.setError("文件名:" + newFile.getPath(), "寫入", e.getMessage());
return null;
}
//判斷是否存在關聯關係
if ((uuid != null) && (!"".equals(uuid))) {
try {
AttachmentInfoModel attachmentInfoModel;
attachmentInfoModel = saveAttachmentInfo(newFileName, name, uuid, type);
if (attachmentInfoModel.attachmentInfoEntity == null) {
this.setError("存儲數據庫失敗", "寫入", "失敗");
return null;
}
} catch (Exception e) {
this.setError("保存數據庫失敗", "寫入", "失敗");
e.printStackTrace();
return null;
}
}
//此處爲返給前臺的url路徑,用來訪問文件,並存取到數據庫當中
String fileUrl = ProjectUtil.getInstance().getSystemInfo().getAttachmentHost() + "/" + newFileName;
this.setInfo("文件訪問路徑:" + fileUrl, "生成", "成功");
fileUploadMap.put("fileUrl", fileUrl);
// }
return fileUploadMap;
}
/**
* 獲取文件名擴展名不包含點
*
* @param fileName 包含擴展名的文件名稱
* @return String 成功返回文件擴展名,失敗返回null
*/
public static String getExtensionName(String fileName) {
int lastDotIndex = 0;
String extensionName = null;
if (fileName == null) {
return null;
}
if (fileName.length() == 0) {
return null;
}
lastDotIndex = fileName.lastIndexOf('.');
//如果文件名當中沒有.的話返回null
if (lastDotIndex == -1) {
return null;
}
//如果.是整個文件名的最後一個則不再能正確認識擴展名
if (lastDotIndex == fileName.length()) {
return null;
}
//得到文件擴展名不包含點
extensionName = fileName.substring(lastDotIndex + 1);
return extensionName;
}
/**
* 獲取文件名擴展名包含點
*
* @param fileName 包含擴展名的文件名稱
* @return String 成功返回文件擴展名,失敗返回null
*/
public static String getDotExtensionName(String fileName) {
String extensionName = null;
String dotExtensionName = null;
extensionName = getExtensionName(fileName);
if (extensionName == null) {
return null;
}
dotExtensionName = "." + extensionName;
return dotExtensionName;
}