file_upload
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" 否则后台接收不到文件
第一次发博客,测试心里居多,以上代码是经测试完全通过,希望看到的大神们多多指点,小弟在这这里谢谢了。