[英]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. 我目前正在使用
JAX-rs
库以Java
开发RESTful web service
。 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 我使用
POSTMAN
作为客户端来测试我的Web服务,并且POSTMAN
以下问题: 当我上传.txt文件时,该文件会附加文件的其他一些详细信息
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? 也许我的请求的“
Headers
部分中缺少某些内容? Or maybe any issue is caused because of the MediaType
I am consuming in my web service endpoint? 还是由于我在Web服务端点中使用的
MediaType
引起了任何问题?
Thanks in advance for any help :) 在此先感谢您的帮助:)
PS PS
If I upload a .pdf file it is not leaded to corruption and the .pdf file is stored normally on my filesystem 如果我上传.pdf文件,则不会导致文件损坏,并且.pdf文件通常存储在我的文件系统中
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
: 最后,我使用
org.apache.cxf
的Attachment
类找到了解决此问题的方法:
@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){
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.