/** * 删除文件 * * @param fileId */ @RequestMapping("deleteFile") public @ResponseBody Map<String, String> deleteFile(String fileId) throws IOException { Map<String, String> map = new HashMap<String, String>(); try { storageClientService.deleteFile(fileId); } catch (Exception e) { e.printStackTrace(); SxjLogger.debug("删除失败!", e.getClass()); } return map; }
/** * 上传文件 * * @param request * @param response * @throws IOException */ @RequestMapping("upload") public void uploadFile(HttpServletRequest request, HttpServletResponse response) throws IOException { Map<String, Object> map = new HashMap<String, Object>(); if (!(request instanceof DefaultMultipartHttpServletRequest)) { return; } DefaultMultipartHttpServletRequest re = (DefaultMultipartHttpServletRequest) request; Map<String, MultipartFile> fileMaps = re.getFileMap(); Collection<MultipartFile> files = fileMaps.values(); List<String> fileIds = new ArrayList<String>(); for (MultipartFile myfile : files) { if (myfile.isEmpty()) { System.err.println("文件未上传"); } else { String originalName = myfile.getOriginalFilename(); String extName = FileUtil.getFileExtName(originalName); String filePath = storageClientService.uploadFile( null, new ByteArrayInputStream(myfile.getBytes()), myfile.getBytes().length, extName.toUpperCase()); SxjLogger.info("siteUploadFilePath=" + filePath, this.getClass()); fileIds.add(filePath); // 上传元数据 NameValuePair[] metaList = new NameValuePair[1]; metaList[0] = new NameValuePair("originalName", originalName); storageClientService.overwriteMetadata(filePath, metaList); } } map.put("fileIds", fileIds); String res = JsonMapper.nonDefaultMapper().toJson(map); response.setContentType("text/plain;UTF-8"); PrintWriter out = response.getWriter(); out.print(res); out.flush(); out.close(); }
/** * 下载文件 * * @param request * @param response * @param filePath * @throws IOException */ @RequestMapping("downloadFile") public void downloadFile( HttpServletRequest request, HttpServletResponse response, String filePath) throws IOException { try { ServletOutputStream output = response.getOutputStream(); String fileName = "扫描件" + stringDate(); String group = filePath.substring(0, filePath.indexOf("/")); response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".pdf"); response.setContentType("application/pdf"); String path = filePath.substring(filePath.indexOf("/") + 1, filePath.length()); storageClientService.downloadFile(group, path, output); output.flush(); output.close(); } catch (IOException e) { e.printStackTrace(); SxjLogger.debug("下载文件出错", e.getClass()); } }
@RequestMapping("filesort") public @ResponseBody List<String> fileSort(String fileId) throws IOException { List<String> sortFile = new ArrayList<String>(); try { if (StringUtils.isEmpty(fileId)) { return sortFile; } String[] fileids = fileId.split(","); Map<String, String> nameMap = new TreeMap<String, String>(); Map<String, NameValuePair[]> values = storageClientService.getMetadata(fileids); for (String key : values.keySet()) { if (key == null) { continue; } NameValuePair[] value = values.get(key); nameMap.put(key, value[0].getValue()); } List<Map.Entry<String, String>> mappingList = null; // 通过ArrayList构造函数把map.entrySet()转换成list mappingList = new ArrayList<Map.Entry<String, String>>(nameMap.entrySet()); // 通过比较器实现比较排序 Collections.sort( mappingList, new Comparator<Map.Entry<String, String>>() { public int compare( Map.Entry<String, String> mapping1, Map.Entry<String, String> mapping2) { return mapping1.getValue().compareTo(mapping2.getValue()); } }); for (Map.Entry<String, String> mapping : mappingList) { sortFile.add(mapping.getKey()); } // Map<String, Object> map = new HashMap<String, Object>(); // map.put("", sortFile); } catch (Exception e) { SxjLogger.error(e.getMessage(), e, this.getClass()); } return sortFile; }