1. 程式人生 > 其它 >檔案上傳前後端例項

檔案上傳前後端例項

前端程式碼

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" value="請選擇檔案">
<input type="submit" value="上傳">
</form>
</body>
</html>



後端程式碼
package com.audittool.controller;

import com.audittool.net.ResponseData;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.*;


/**
*
* 檔案上傳處理類
*
*/


@RestController
public class FileController extends HttpServlet {
//記錄錯誤資訊
private static final Logger log = LoggerFactory.getLogger(BasicInformationController.class);
SimpleDateFormat sd = new SimpleDateFormat("yyyy/MM/dd");
@PostMapping("/upload")
public String upload(MultipartFile uploadFile, HttpServletRequest req){
String realPath = "D:\\專案庫\\State Grid Audit Dashboard\\src\\main\\resources\\upload";
/* String format = sd.format(new Date());*/
File file = new File(realPath);
if (!file.isDirectory()){
file.mkdirs();
}
// 獲取附件原名(有的瀏覽器如chrome獲取到的是最基本的含 字尾的檔名,如myImage.png)
// 獲取附件原名(有的瀏覽器如ie獲取到的是含整個路徑的含字尾的檔名,如C:\\Users\\images\\myImage.png)
String fileName = uploadFile.getOriginalFilename();
// 如果是獲取的含有路徑的檔名,那麼擷取掉多餘的,只剩下檔名和字尾名
/*int index = fileName.lastIndexOf("\\");
if (index > 0) {
fileName = fileName.substring(index + 1);
}*/
// 判斷單個檔案大於1M
long fileSize = uploadFile.getSize();
if (fileSize > 1024 * 1024) {
System.out.println("檔案大小為(單位位元組):" + fileSize);
System.out.println("該檔案大於1M");
}
// 當檔案有後綴名時
if (fileName.indexOf(".") >= 0) {
// split()中放正則表示式; 轉義字元"\\."代表 "."
String[] fileNameSplitArray = fileName.split("\\.");
/*// 加上random戳,防止附件重名覆蓋原檔案
fileName = fileNameSplitArray[0] + (int) (Math.random() * 100000) + "." + fileNameSplitArray[1];*/
}
// 當檔案無後綴名時(如C盤下的hosts檔案就沒有後綴名)
if (fileName.indexOf(".") < 0) {
// 加上random戳,防止附件重名覆蓋原檔案
fileName = fileName + (int) (Math.random() * 100000);
}
System.out.println("fileName:" + fileName);

// 根據檔案的全路徑名字(含路徑、字尾),new一個File物件dest
File dest = new File(realPath + fileName);
// 如果該檔案的上級資料夾不存在,則建立該檔案的上級資料夾及其祖輩級資料夾;
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
// 將獲取到的附件file,transferTo寫入到指定的位置(即:建立dest時,指定的路徑)
uploadFile.transferTo(new File(file,fileName));
System.out.println("檔案的全路徑名字(含路徑、字尾)>>>>>>>" + realPath+"檔名稱:" + fileName);
return "上傳成功";
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "上傳失敗";
}
}