简体   繁体   中英

Uploaded .txt file through POSTMAN gets corrupted (appended with Content-Disposition, Content-type) JAX-RS

Greetings to the community! I am currently developing a RESTful web service in Java using the JAX-rs library. What I would like to do is make clients able to upload a file via the service. I successfully managed to achieve this using the following piece of code

@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)
}

Implementation class

@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();
    }

 }
}

I am using POSTMAN as a client to test my web service and I am facing the following problem: When I upload a .txt file, that files gets appended with some other details of the file

Example:

File sent

文件已发送

Postman Request

邮递员要求

File stored on my filesystem

文件存储

Any idea why this happens? Maybe I am missing something in the Headers section of my request? Or maybe any issue is caused because of the MediaType I am consuming in my web service endpoint?

Thanks in advance for any help :)

PS

If I upload a .pdf file it is not leaded to corruption and the .pdf file is stored normally on my filesystem

Your method signature should be

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

and you can get content of the file as

InputStream uploadedInputStream = uploadedFile.getValueAs(InputStream.class)

Hope this helps.

Finally I found a workaround for my problem using the Attachment class of org.apache.cxf :

@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){

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM