如何修复剩余获取,文件下载有时显示找不到文件错误

时间:2019-05-13 09:59:53

标签: java rest jax-rs

因此,我尝试使用rest get方法下载之前上传的文件,但有时我发现文件未找到错误,而其他时候它仍然可以正常工作。我不知道问题出在哪里。

我还尝试了该线程file downloading in restful web services中的代码,并对其进行了一些更改,但是此代码的问题是我看不到文件的内容,所以我认为我的解决方案要好一些

@GET
  @Path("/{fileName}")
  @Produces(MediaType.APPLICATION_OCTET_STREAM)
  public Response downloadFile(@PathParam("fileName") String fileName) throws IOException
  {
    File ivyFile = new File(fileName);
    byte[] data = ivyFile.readBinary().toByteArray();
    StreamingOutput fileStream = new StreamingOutput()
      {
        @Override
        public void write(java.io.OutputStream output)
        {
          try
          {
            output.write(data);
            output.flush();
          }
          catch (IOException e)
          {
            throw new WebApplicationException("Could not Find the file: '" + fileName + "'", e);
          }
        }
      };
    return Response.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
            .header("content-disposition", "attachment; filename = " + fileName).build();
  }

我希望下载包含内容的文件并能够看到它。

编辑: 当此错误发生时,我也会得到响应200。还有这个错误:

MalformedChunkCodingException: CRLF expected at end of chunk
    FacesException: org.apache.http.MalformedChunkCodingException: CRLF expected at end of chunk
    Error during rendering of html dialog 'com.axonivy.connectivity.rest.FileUpload'

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。问题出在我的IDE上,导致下载超时。