<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8 ...
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.Properties; import org.apache.commons.fileupload.disk.DiskFileItem; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; /** * file upload * * @author ForeignStudent * @version 2017/9/21 */ public class FileUpload { public static final String SLASHES = "/"; public static final String DOT = "."; //properties path properties文件在項目中的位置 public static final String PROPERTIES_PATH = "applicationContext/spring.properties"; //static initial path 文件保存的路徑 public static final String IMG_UPLOAD_SAVEPATH = "img_upload_savePath"; //session length 位元組流一次讀取的長度 public static final String IMG_UPLOAD_LENGTH = "img_upload_length"; //file shunt path 文件保存位置(因為文件不可能只有一種用途,所以添加分流,可以在下麵繼續添加,和properties文件屬性保持一致) public static final String SHUNT_PATH = "shunt_path"; /** * file upload * * @param multipartFile spring up file * @param savePath save path
* @param databasePath shunt path */ @SuppressWarnings("deprecation") public static String fileSave(MultipartFile multipartFile, String savePath, String databasePath) { // will be create file name 生成一個文件名稱,這裡使用uuid,當然也可以使用其他,本人習慣uuid String imgName = StringUtils.getUUId(); // Strong conversions file spring自帶文件上傳沒有細研究,所有強轉成file進行處理 CommonsMultipartFile cf = (CommonsMultipartFile) multipartFile; DiskFileItem fi = (DiskFileItem) cf.getFileItem(); File file = fi.getStoreLocation(); // file name 上傳文件的名稱,截取尾碼時用 String fileName = multipartFile.getOriginalFilename(); // file prefix 上傳文件的尾碼,這裡沒有限制上傳格式,最好在其他層面判斷 String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
// 生成日期,獲取月和日,作為文件夾來使用,最好是加上年,這裡沒加 Date date = new Date(); int month = date.getMonth(); int day = date.getDay(); String months = String.valueOf(month) + "month"; String days = String.valueOf(day) + "day";
// 獲取properties配置的讀取長度,把長度寫成可配置就可以隨情況而確定要多大長度 int length = Integer.parseInt(readProperties(IMG_UPLOAD_LENGTH)); byte[] buffer = new byte[length]; int numberRead = 0; FileInputStream input = null; FileOutputStream out = null; try {
//生成要創建的文件夾路徑 String savePaths = savePath + SLASHES + databasePath + SLASHES + months + SLASHES + days; input = new FileInputStream(file);
//這裡創建文件夾,存在就不創建,不存在就創建 createFile(savePaths); out = new FileOutputStream(savePaths + SLASHES + imgName + DOT + prefix); while ((numberRead = input.read(buffer)) != -1) { out.write(buffer, 0, numberRead); }
//返迴文件路徑,不帶盤符,可以使用靜態資源訪問文件 return databasePath + SLASHES + months + SLASHES + days + SLASHES + imgName + DOT + prefix; } catch (final IOException e) { e.printStackTrace(); } finally { try { input.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * create folders * * @param path save path */ public static void createFile(String savePath) { File file = new File(savePath); boolean exists = file.exists();
//判斷路徑是否存在 if (!exists) { boolean mkdir = file.mkdirs(); System.out.println(mkdir); } } /** * read properties file * * @param attribute * @return */ public static String readProperties(String attribute) { Properties properties = new Properties(); try {
//載入properties文件,可以通過getproperty()來獲取屬性 properties.load(FileUpload.class.getClassLoader().getResourceAsStream(PROPERTIES_PATH)); return properties.getProperty(attribute); } catch (IOException e) { e.printStackTrace(); } return null; } // Call the instance 調用實例 // public String imgSave(MultipartFile multipartFile) { // String propertiesValue = FileUpload.readProperties(FileUpload.IMG_UPLOAD_SAVEPATH); // String readProperties = FileUpload.readProperties(FileUpload.SHUNT_PATH); // String fileSave = FileUpload.fileSave(multipartFile, propertiesValue, readProperties); // return fileSave; // } }
spring 上傳文件功能實現得配置 如下bean
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<!--1024*200即200k-->
<!-- 1024*1024即1M -->
<property name="maxUploadSize" value="1024*1024" />
<!-- <property name="maxInMemorySize" value="40960" /> -->
</bean>
同時在頁面的from裡面加上 enctype="multipart/form-data" 否則後臺接收不到文件
第一次發博客,測試心裡居多,以上代碼是經測試完全通過,希望看到的大神們多多指點,小弟在這這裡謝謝了。