示例#1
0
 // 文件的下载
 @RequestMapping("download")
 public ResponseEntity<byte[]> download() throws IOException {
   File file = new File("d:/PLSQLDeveloper.zip");
   HttpHeaders headers = new HttpHeaders();
   headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
   headers.setContentDispositionFormData("attachment", "dict.txt");
   headers.setContentLength(file.length());
   return new ResponseEntity<byte[]>(
       FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
 }
  /** Upload single file using Spring Controller */
  @RequestMapping(
      value = "/uploadFile",
      method = RequestMethod.POST,
      produces = "application/json;charset=utf8")
  @ResponseBody
  public FileMsg uploadFileHandler(@RequestParam("file") MultipartFile file) throws IOException {

    if (!file.isEmpty()) {
      InputStream in = null;
      OutputStream out = null;

      try {
        // 获得在tomcat中项目的路径, 需要在web.xml配置ft.webapp
        String webRootPath = System.getProperty("ft.webapp");
        // logger.info(webRootPath);
        // String rootPath = System.getProperty("catalina.home");
        File dir = new File(webRootPath + File.separator + "uploadFiles");
        if (!dir.exists()) dir.mkdirs();
        File serverFile =
            new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
        in = file.getInputStream();
        out = new FileOutputStream(serverFile);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) > 0) {
          out.write(b, 0, len);
        }
        out.close();
        in.close();
        logger.info("Server File Location=" + serverFile.getAbsolutePath());

        FileMeta fileMeta = new FileMeta();
        fileMeta.setName(file.getOriginalFilename());
        fileMeta.setUrl("uploadFiles" + File.separator + file.getOriginalFilename());
        List<FileMeta> files = new ArrayList<>();
        files.add(fileMeta);
        FileMsg fileMsg = new FileMsg();
        fileMsg.setFiles(files);
        return fileMsg;

      } catch (Exception e) {
        FileMeta fileMeta = new FileMeta();
        fileMeta.setName(file.getOriginalFilename());
        fileMeta.setUrl("uploadFiles" + File.separator + file.getOriginalFilename());
        List<FileMeta> files = new ArrayList<>();
        files.add(fileMeta);
        FileMsg fileMsg = new FileMsg();
        fileMsg.setFiles(files);
        return fileMsg;
      } finally {
        if (out != null) {
          out.close();
          out = null;
        }

        if (in != null) {
          in.close();
          in = null;
        }
      }
    } else {
      FileMeta fileMeta = new FileMeta();
      fileMeta.setName(file.getOriginalFilename());
      fileMeta.setUrl("uploadFiles" + File.separator + file.getOriginalFilename());
      List<FileMeta> files = new ArrayList<>();
      files.add(fileMeta);
      FileMsg fileMsg = new FileMsg();
      fileMsg.setFiles(files);
      return fileMsg;
    }
  }