| | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * CommonsFileupload上传服务 |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class CommonsFileuploadService { |
| | | @Autowired |
| | | PathHelper pathHelper; |
| | |
| | | return copeFileItems(items, req); |
| | | } |
| | | |
| | | /** |
| | | * 获取文件项 |
| | | */ |
| | | private List<FileItem> getFileItem(HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | // 处理中文乱码问题 |
| | | req.setCharacterEncoding("utf-8"); |
| | | // text/html;charset=utf-8 |
| | | res.setContentType("application/json;charset=utf-8"); |
| | | |
| | | // 检查请求是/否为multipart/form-data类型 |
| | | if (!ServletFileUpload.isMultipartContent(req)) { |
| | | throw new RuntimeException("表单的enctype属性不是multipart/form-data类型"); |
| | | } |
| | | |
| | | // 创建上传所需要的两个对象:磁盘文件对象+文件上传对象 |
| | | DiskFileItemFactory factory = new DiskFileItemFactory(); |
| | | ServletFileUpload sfu = new ServletFileUpload(factory); |
| | | ServletRequestContext ctx = new ServletRequestContext(req); |
| | | |
| | | // 设置编码方式 |
| | | sfu.setHeaderEncoding("utf-8"); |
| | | factory.setSizeThreshold(4096); |
| | | |
| | | // 获取表单中的所有数据信息 |
| | | List<FileItem> list = sfu.parseRequest(ctx); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 处理解析内容:处理普通表单域和文件表单域 |
| | | */ |
| | | private Object copeFileItems(List<FileItem> items, HttpServletRequest req) throws Exception { |
| | | Map<String, String> map = new HashMap<String, String>(3); |
| | | List<FileInfoEntity> list = new ArrayList<FileInfoEntity>(); |
| | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 处理文件 |
| | | */ |
| | | private FileInfoEntity copeFile(FileItem item, String path, HttpServletRequest req) { |
| | | try { |
| | | // 获取文件名,判断是否合法 |
| | | FileInfoEntity fi = new FileInfoEntity(item.getName()); |
| | | if (StringHelper.isEmpty(fi.getFileName())) { |
| | | return null; |