【问题标题】:Uploaded .txt file through POSTMAN gets corrupted (appended with Content-Disposition, Content-type) JAX-RS通过 POSTMAN 上传的 .txt 文件已损坏(附加 Content-Disposition、Content-type) JAX-RS
【发布时间】:2019-05-20 23:42:37
【问题描述】:

向社区致敬!我目前正在使用JAX-rs 库在Java 中开发RESTful web service。我想做的是让客户能够通过服务上传文件。我成功地使用以下代码实现了这一目标

@Consumes({"application/json"})
@Produces({"application/json"})
@Path("uploadfileservice")
public interface UploadFileService {

   @Path("/fileupload")
   @POST
   @Consumes(MediaType.MULTIPART_FORM_DATA)
   Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream)
}

实现类

@Service
public class UploadFileServiceImpl implements UploadFileService {

@Override
public Response uploadFile(InputStream uploadedInputStream){
String fileToWrite = "//path/file.txt" //assuming a upload a txt file
writeToFile(uploadedInputStream, fileToWrite); //write the file
}

private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    try {
        OutputStream out = new FileOutputStream(new File(
                uploadedFileLocation));
        int read = 0;
        byte[] bytes = new byte[1024];

        out = new FileOutputStream(new File(uploadedFileLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

 }
}

我正在使用POSTMAN 作为客户端来测试我的网络服务,但我遇到了以下问题:当我上传 .txt 文件时,该文件会附加该文件的一些其他详细信息

例子:

文件发送

邮递员请求

文件存储在我的文件系统中

知道为什么会这样吗?也许我在我的请求的Headers 部分遗漏了一些东西?或者,我在 Web 服务端点中使用的 MediaType 可能会导致任何问题?

提前感谢您的帮助:)

附言

如果我上传 .pdf 文件,它不会导致损坏,并且 .pdf 文件会正常存储在我的文件系统中

【问题讨论】:

    标签: java rest file-upload jax-rs postman


    【解决方案1】:

    你的方法签名应该是

    Response uploadFile(@FormDataParam("file") FormDataBodyPart uploadedFile)
    

    你可以得到文件的内容

    InputStream uploadedInputStream = uploadedFile.getValueAs(InputStream.class)
    

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回复,我应该在我的 POSTMAN 请求中包含任何特殊的标头吗?因为我添加了 Content-Type = multipart/form-data 而根本没有 Headers 并收到 415 Unsupported Media Type 异常
    • 在 POSTMAN 中,如果您使用 multipart/form-data,则不必设置 Content-Type 标头。详情请参考github.com/postmanlabs/postman-app-support/issues/191
    【解决方案2】:

    最后我找到了使用org.apache.cxfAttachment 类解决我的问题的方法:

    @Consumes({"application/json"})
    @Produces({"application/json"})
    @Path("uploadfileservice")
    public interface UploadFileService {
    
    @Path("/fileupload")
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    Response uploadFile(@Multipart("file") Attachment attr)
    }
    
    
    
    @Service
    public class UploadFileServiceImpl implements UploadFileService {
    
    @Override
    public Response uploadFile(Attachment attr){
    String pathToUpload= "//path//.txt"
    try{
      attr.transferTo(new File(pathToUpload)); //will copy the uploaded file in 
      //this destination
    }
    catch(Exception e){
    
    }
    
    }
    

    【讨论】:

    • 这不是一种解决方法。如果您使用的是 CXF,@FormDataParam 将不起作用,因为那是针对 Jersey 的。您使用的 @Multipart 用于 CXF。这些是不同的实现,每个实现的多部分支持不同且不兼容。
    • 但是你可以看到我不再使用 FormDataParam,我把它改成了 org.apache.cxf.jaxrs.ext.multipart.Multipart
    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 2018-06-10
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2021-12-22
    • 2016-12-18
    • 1970-01-01
    相关资源
    最近更新 更多