Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用spring控制器处理文件_Spring_File_Spring Boot_Controller - Fatal编程技术网

使用spring控制器处理文件

使用spring控制器处理文件,spring,file,spring-boot,controller,Spring,File,Spring Boot,Controller,我需要:通过@Controller从我的文件系统下载或显示内联文件 我找到了好几种方法。但是,我不知道哪一个更好 我在读这样的文件: public static byte[] readFileAsByteArray(String fileName) { ... return Files.readAllBytes(Paths.get(fileName)); ... } 方法1(效果良好): @RequestMapping(value = "/help/manual") publ

我需要:通过
@Controller
从我的文件系统下载或显示内联文件

我找到了好几种方法。但是,我不知道哪一个更好

我在读这样的文件:

public static byte[] readFileAsByteArray(String fileName) {
   ...
   return Files.readAllBytes(Paths.get(fileName));
   ...
}
方法1(效果良好):

@RequestMapping(value = "/help/manual")
public ResponseEntity<byte[]> getUsersManual() {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    headers.add("content-disposition", "inline;filename=" + MANUAL_FILE);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    byte[] fileBytes = FileUtils.readFileAsByteArray(MANUAL_FILE);
    ResponseEntity<byte[]> response = new ResponseEntity<>(fileBytes, headers, HttpStatus.OK);
    return response;
}
@RequestMapping(value = "/help/manual1")
public void getUsersManual1(HttpServletResponse response) throws IOException {
    ServletOutputStream outputStream = response.getOutputStream();
    response.setContentType("application/pdf");
    response.addHeader("content-disposition", "inline;filename=" + MANUAL_FILE);
    outputStream.write(FileUtils.readFileAsByteArray(MANUAL_FILE));
    outputStream.flush();
    outputStream.close();
}
@RequestMapping(value = "/help/manual2")
@ResponseBody
public FileSystemResource getUsersManual2(HttpServletResponse response) {
    File file = new File(MANUAL_FILE);
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
    response.setHeader("Content-Length", String.valueOf(file.length()));
    return new FileSystemResource(file);
}
方法3(仅适用于“应用程序/八位字节流”和附件):

@RequestMapping(value = "/help/manual")
public ResponseEntity<byte[]> getUsersManual() {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    headers.add("content-disposition", "inline;filename=" + MANUAL_FILE);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    byte[] fileBytes = FileUtils.readFileAsByteArray(MANUAL_FILE);
    ResponseEntity<byte[]> response = new ResponseEntity<>(fileBytes, headers, HttpStatus.OK);
    return response;
}
@RequestMapping(value = "/help/manual1")
public void getUsersManual1(HttpServletResponse response) throws IOException {
    ServletOutputStream outputStream = response.getOutputStream();
    response.setContentType("application/pdf");
    response.addHeader("content-disposition", "inline;filename=" + MANUAL_FILE);
    outputStream.write(FileUtils.readFileAsByteArray(MANUAL_FILE));
    outputStream.flush();
    outputStream.close();
}
@RequestMapping(value = "/help/manual2")
@ResponseBody
public FileSystemResource getUsersManual2(HttpServletResponse response) {
    File file = new File(MANUAL_FILE);
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
    response.setHeader("Content-Length", String.valueOf(file.length()));
    return new FileSystemResource(file);
}
那么,在
@Controller
中处理文件的最佳方法是什么

我使用:

  • 弹簧靴1.5.9
  • 春季4.3.13

谢谢。

我更喜欢方法2稍加修改。
如果文件大小很大,例如几GB,会发生什么。
因此,您应该使用
InputStream
读取该文件,并将其写入servlet
OutputStream
。否则,由于堆空间的原因,您可能会捕获到
outOfMemoryException

@RequestMapping(value = "/help/manual1")
public void getUsersManual1(HttpServletResponse response) throws IOException {

    String MANUAL_FILE="yourfileName";
    int BUFFER_SIZE = 4096;
    // the complete absolute path of the file
    String fullPath = "path\\to\\your\\file";
    File downloadableFile = new File(fullPath);
    FileInputStream inputStream = new FileInputStream(downloadableFile);

    ServletOutputStream outputStream = response.getOutputStream();
    response.setContentType("application/pdf");
    response.addHeader("content-disposition", "inline;filename=" + MANUAL_FILE);
    response.setContentLength((int) downloadableFile.length());

    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead = -1;

    // we need to write the bytes which we read from inputStream
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }

    outputStream.flush();
    outputStream.close();
    inputStream.close();
}

我更喜欢方法2,稍加修改。
如果文件大小很大,例如几GB,会发生什么。
因此,您应该使用
InputStream
读取该文件,并将其写入servlet
OutputStream
。否则,由于堆空间的原因,您可能会捕获到
outOfMemoryException

@RequestMapping(value = "/help/manual1")
public void getUsersManual1(HttpServletResponse response) throws IOException {

    String MANUAL_FILE="yourfileName";
    int BUFFER_SIZE = 4096;
    // the complete absolute path of the file
    String fullPath = "path\\to\\your\\file";
    File downloadableFile = new File(fullPath);
    FileInputStream inputStream = new FileInputStream(downloadableFile);

    ServletOutputStream outputStream = response.getOutputStream();
    response.setContentType("application/pdf");
    response.addHeader("content-disposition", "inline;filename=" + MANUAL_FILE);
    response.setContentLength((int) downloadableFile.length());

    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead = -1;

    // we need to write the bytes which we read from inputStream
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }

    outputStream.flush();
    outputStream.close();
    inputStream.close();
}

您曾写道希望通过@Controller实现这一点,但您是否考虑过spring ResourceHandler,它是spring提供静态资源的最佳方式

以下是一个例子:

您曾写道希望通过@Controller实现这一点,但您是否考虑过spring ResourceHandler,它是spring提供静态资源的最佳方式

以下是一个例子:

属于属于属于属于属于属于