因為開發環境和線上環境系統不一樣,所以需要區別環境
config.java
可以判斷系統進行自動化的區別,我是手動去切換注釋的
public class config {
//public static final String filePath = "E:\\nginx-1.16.1\\html\\uploadFile";//本地
public static final String filePath = "/usr/local/nginx/html/uploadFile/"; //線上
//public static final String fileUrl = "http://localhost/uploadFile/"; //本地
public static final String fileUrl = "http://xx.xx.xx.xx/uploadFile/"; //線上
}
工具
// util/uuid.java
public class uuid {
public static String getUuid(){
String uuid = UUID.randomUUID().toString();
//去掉“-”符號
return uuid.replaceAll("-", "");
}
}
上傳文件
// util/UploadFile.java
@Controller
public class UploadFile {
@RequestMapping("/uploadFile")
@ResponseBody
public Map upload(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws Exception, IOException {
return saveFile(file, req);
}
private Map saveFile(MultipartFile file, HttpServletRequest req) {
Map map = new HashMap();
// 判斷文件是否為空
if (!file.isEmpty()) {
try {
String fileId = uuid.getUuid();
String fileName = file.getOriginalFilename();//1.jpg 獲取上傳文件的全名加后綴
map.put("fileId",fileId);
map.put("fileName",fileName);
//這是指定的絕對路徑
String filePath = config.filePath;
String fileUrl = config.fileUrl;
File realPathFolder = new File(filePath);
if (!realPathFolder.exists()) {
realPathFolder.mkdir();
}
String newName = fileId + "_" + fileName;//創建一個新的文件名稱 FilenameUtils.getExtension(name):獲取文件后綴名
File f = new File(realPathFolder,newName);
map.put("newName", newName);
map.put("fileUrl",fileUrl);
map.put("url",fileUrl+newName);
map.put("code", 20000); //成功的固定字段
map.put("msg", "上傳file成功");
file.transferTo(f);//將上傳的文件存儲到指定位置
} catch (Exception e) {
map.put("code", 0);
map.put("msg", "上傳file失敗");
e.printStackTrace();
}
} else {
map.put("msg", "上傳file失敗");
}
return map;
}
}
上傳base64
// util/UpLoadBase64.java
@Controller
public class UpLoadBase64 {
@RequestMapping(value = "/uploadBase64", method = RequestMethod.POST)
@ResponseBody
public Map base64UpLoad(@RequestParam String file,@RequestParam String fileName,HttpServletRequest req, HttpServletResponse res) {
Map map = new HashMap();
try {
String dataPrix = "";
String data = "";
if (file == null || "".equals(file)) {
throw new Exception("上傳數據為空");
} else {
String[] d = file.split("base64,"); //把base, 切換
if (d != null && d.length == 2) {
dataPrix = d[0];
data = d[1];
} else {
throw new Exception("上傳失敗,數據不合法");
}
}
String fileId = uuid.getUuid();
map.put("fileId", fileId);
map.put("fileName", fileName);
//這是指定的絕對路徑
String filePath = config.filePath;
String fileUrl = config.fileUrl;
//因為BASE64Decoder的jar問題,此處使用spring框架提供的工具包
byte[] bs = Base64Utils.decodeFromString(data);
File realPathFolder = new File(filePath);
if (!realPathFolder.exists()) {
realPathFolder.mkdir();
}
String newName = fileId + '_' + fileName;//創建一個新的文件名稱.后綴名
FileUtils.writeByteArrayToFile(new File(realPathFolder,newName), bs);
map.put("newName", newName);
map.put("fileUrl",fileUrl);
map.put("url",fileUrl+newName);
map.put("code", 20000); //成功的固定字段
map.put("msg", "上傳base64成功");
} catch (Exception e) {
map.put("msg", "上傳base64失敗");
e.printStackTrace();
}
return map;
}
}
刪除文件
// util/DeleteFile.java
@Controller
public class DeleteFile {
@RequestMapping("/deleteFile")
@ResponseBody //不寫會默認返回當前路徑!!
public Map deleteFile(@RequestParam("fileName") String fileName) throws Exception, IOException {
Map map = new HashMap();
File file = new File(filePath,fileName);
if(file.exists()){
file.delete();
map.put("code",20000);
map.put("msg","文件刪除成功");
}else{
map.put("msg","文件不存在");
}
return map;
}
}
文件下載
// util/DownLoad.java
@Controller
public class DownLoad {
/*
* 下載方式一:
* ①獲取前台要下載的文件名稱
* ②設置響應類型
* ③設置下載頁顯示的文件名
* ④獲取下載文件夾的絕對路徑和文件名合並為File類型
* ⑤將文件復制到瀏覽器
*/
@RequestMapping("/download")
@ResponseBody
public void download(HttpServletRequest req, HttpServletResponse resp, String fileName) throws Exception {
String filePath = config.filePath;
//String realPath = req.getServletContext().getRealPath("/download");//獲取下載文件的路徑
File file = new File(filePath,fileName);//把下載文件構成一個文件處理 filename:前台傳過來的文件名稱
//設置響應類型 ==》 告訴瀏覽器當前是下載操作,我要下載東西
resp.setContentType("application/x-msdownload");
//設置下載時文件的顯示類型(即文件名稱-后綴) ex:txt為文本類型
resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//下載文件:將一個路徑下的文件數據轉到一個輸出流中,也就是把服務器文件通過流寫(復制)到瀏覽器端
Files.copy(file.toPath(), resp.getOutputStream());//Files.copy(要下載的文件的路徑,響應的輸出流)
}
}
這些代碼隨便百度就有,還有上傳多個文件的,自行百度