在Java中实现文件的上传有多种方式,如smartUpload或是使用Strus2,本文与大家分享使用spring框架中的MultipartFile类来实例文件的上传。不啰嗦了,直接上干货。先是编写了一个实现文件上传的类FileUpload
在Java中实现文件的上传有多种方式,如smartUpload或是使用Strus2,本文与大家分享使用spring框架中的MultipartFile类来实例文件的上传。
不啰嗦了,直接上干货。先是编写了一个实现文件上传的类FileUploadingUtil,此类中定义了两个对外公开的方法,upload和getFileMap。
前者需要传入一个Map参数,是用户提交的表单中的文件列表,最终返回值的也是一个Map类型对象,其键名为上传的文件名称,键值为文件在服务器上的存储路径;后者主要是用于测试用途,非主要功能,看官可以忽略此方法。
package com.emerson.cwms.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import org.springframework.WEB.multipart.MultipartFile; public class FileUploadingUtil { public static String FILEDIR = null; public static Map<String, String> upload(Map<String, MultipartFile> files) throws IOException { File file = new File(FILEDIR); if (!file.exists()) { file.mkdir(); } Map<String, String> result = new HashMap<String, String>(); Iterator<Entry<String, MultipartFile>> iter = files.entrySet().iterator(); while (iter.hasNext()) { MultipartFile aFile = iter.next().getValue(); if (aFile.getSize() != 0 && !"".equals(aFile.getName())) { result.put(aFile.getOriginalFilename(), uploadFile(aFile)); } } return result; } private static String uploadFile(MultipartFile aFile) throws IOException { String filePath = initFilePath(aFile.getOriginalFilename()); try { write(aFile.getInputStream(), new FileOutputStream(filePath)); } catch (FileNotFoundException e) { logger.error("上传的文件: " + aFile.getName() + " 不存在!!"); e.printStackTrace(); } return filePath; } private static void write(InputStream in, OutputStream out) throws IOException { try { byte[] buffer = new byte[1024]; int bytesRead = -1; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); } finally { try { in.close(); out.close(); } catch (IOException ex) { } } } public static Map<String, String> getFileMap() { logger.info(FileUploadingUtil.FILEDIR); Map<String, String> map = new HashMap<String, String>(); File[] files = new File(FileUploadingUtil.FILEDIR).listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { File[] files2 = file.listFiles(); if (files2 != null) { for (File file2 : files2) { String name = file2.getName(); logger.info(file2.getParentFile().getAbsolutePath()); logger.info(file2.getAbsolutePath()); map.put(file2.getParentFile().getName() + "/" + name, name.substring(name.lastIndexOf("_") + 1)); } } } } } return map; } private static String initFilePath(String name) { String dir = getFileDir(name) + ""; File file = new File(FILEDIR + dir); if (!file.exists()) { file.mkdir(); } Long num = new Date().getTime(); Double d = Math.random() * num; return (file.getPath() + "/" + num + d.longValue() + "_" + name).replaceAll(" ", "-"); } private static int getFileDir(String name) { return name.hashCode() & 0xf; } }
--结束END--
本文标题: Spring框架实现文件上传功能
本文链接: https://www.lsjlt.com/news/221309.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0