How can I fix org.jvnet.mimepull.MIMEParsingException? - java

While uploading a image/doc/xlsx file from my AngularJS client to my server-side java using JAX-RS(Jersey) i am getting the following exception,
org.jvnet.mimepull.MIMEParsingException: Reached EOF, but there is no closing MIME boundary.
What is this? Why I am getting this exception? How can I get rid of this?
Note: It works for the files with extension .txt, .html, .yml, .java, .properties
But not working for the for the file with extension .doc, .xlsx, .png, .PNG, .jpeg.. etc.
My Server side code:
#POST
#Path("/{name}")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadedFiles(#Nonnull #PathParam("name") final String name,
#FormDataParam("file") final InputStream inputStream,
#FormDataParam("file") final FormDataContentDisposition content) {
}

I encountered the same issue. Based on my research, the problem has no relation with the file type. It has a little relation with the size of the uploaded file.
I'm not sure if the root cause is when the uploading file is very big, before the file is uploaded to the server completely, the client disconnects to the server (such as timeout). And I also verified the guess. My test steps is,
1. In client, upload a very big file.
2. Before the get the response from server, which means is uploading file;
close the test client
3. check the server side, you will see the issue.
So To fix it, my solution is add timeout time in client side.

OK, I'm only guessing, but I think I can see a pattern here.
The file types that are working are text based
The file types that are not working are binary
This suggests to me that maybe the problem is that there is some kind of issue with the way that non-text data is being handled by the upload process. Maybe it is being transcoded when it shouldn't be.
Anyway, I suggest that you use some tool like Wireshark to capture the TCP/IP traffic in an upload to see if the upload request body has valid MIME encapsulation.

Related

How to correctly upload an attachment with Apache HTTPClient to RTC (IBM Jazz)?

I am currently facing the following issue with a Java program.
I am trying to upload a file I downloaded from an external server (file type can be different for every execution) to a IBM RTC (or Jazz) server through REST call.
The REST endpoint I am sending the first request (i.e.: the file upload to the central repository) is as the following:
https://jazz.host.com:1234/ccm/service/com.ibm.team.workitem.service.internal.rest.IAttachmentRestService/?projectId=<TEAM_AREA_ID>&multiple=true&category=<CATEGORY_ID>
And this step works, uploading the file to the central repo. What it is not working is the fact that when the attachment name contains some kind of characters (i.e.: £, ù, °, ...) those are changed into the character ?.
I am using Apache HttpClient with a CloseableHttpClient, preparing the HttpEntity to be sent as follows:
java.io.File currentAttachmentFile = utils.getFileFromPath(attachmentPath);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", currentAttachmentFile, ContentType.MULTIPART_FORM_DATA, currentAttachmentFile.getName());
return builder.build();
But even sepcifying the withCharset(StandardCharsets.UTF8) to ContentType.MULTIPART_FORM_DATA does not do the trick, the file name stile contains the ? character (while I expect it to be something like: 'file_name 1!==£$%&^+ù°##'.png').
I took a look around for other suggestions but neither those found here and here are working for the file name. Moreover, specifying the Content-Type to the POST call I am sending causes a failure with the message 'Invalid POST request message' returned by RTC, so this can't be an option.
Through PostMan, the upload with the same file to the same endpoint works so I suppose there is something I am missing when creating the entity to send to RTC.
Does anyone know how to solve this?
Best regards,
Alberto

writing file content into a http response

How can I write a content of a file into http response? I prefer to write it to an entity first if possible. I searched for examples, but unfortunately didn't find any suitable working one..
Of course it is possible. After all that's what all web servers do when they serve you pages. Add proper Content-Type and Content-Lenght (if known) to your headers, open your file, read it and write it your response.

REST File uploading - multipart or just sending content on inputstream

I need to write REST resource that should receive a file and save it to the disk.
The files will be sent from jersey client.
For now, I see two options:
1. Using multipart
2. Just reading the inputstream as a string and saving it to a file.
What are the pros of using multipart? is it related to file size? or anything else?
Thanks
If you use Jersey server side, using multipart you gain
disk buffering (surely you don't want to retain huge files in memory)
automatic base64/binary stream conversion
If you choose the String option these benefits are unavailable.
See also my answer to the question JAX-RS Accept Images as input, there is a sample implementation of the multipart option

How to Upload File through XML Webservice using Java

I have read plenty of explanations about how to upload files to a server through XML using C#, but I have not found how to do the same using Java and have not been able to figure it out at all. I wonder if anybody on you have done this job successfully.
My intention is to do an HTTP posts containing some text values as a caption and description of an image file, then post them and the image file to the web server through XML.
It's very easy to use Jersey to upload files. Actually, it will enable you to access file content as a InputStream. See sample code below:
#Consumes("multipart/form-data")
#POST
public void post(#FormParam("file") InputStream file) {
...
}
For client side, Jersey can also help you do send HTTP request. A good sample can be found here: http://www.tuple23.com/2010/03/file-upload-using-jersey-client.html

Delete file from a FTP server using Java

I am writing an application that involves multiple clients. One client uploads a file using FTP to a server, and then another client downloads the file and deletes it. I am using the FTP server kind of as a middleman to exchange information because I do not want the user to have to port forward. I have figured out how to upload a file, but I cannot figure out how to delete the file. The command for deleting a file using FTP is:
DELE <filename>
I have tried doing so, but with no success. Here is the code that I have tried:
public static void deleteFile(String name) throws IOException
{
URL url = new URL("ftp://a1111111:password#mywebsite.com/public_html/misc/screenshots/picture.png;type=i");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(url.openConnection().getOutputStream()));
writer.write("DELE picture.png");
writer.flush();
}
I have a feeling that the < filename > that I provided may be wrong because I did not include the directory names in the path. However, since I explicitly told the path that of the file in the URL, I am not quite sure what I am supposed to enter for the < filename >.
I have read other questions on this web site about problems very similar to this and all of the responses tell to use a library. The thing is, most of those libraries are written in pure Java so the developers of that library had to figure out a way to do what I am trying to do without a library. I want to do that as well. I do not like attaching extra files besides my own to the things that I make. So please, do not tell me to use a library - it's not what I'm looking for.
If you need any clarification, please ask!
EDIT: If I use this code to receive a response:
byte[] response = new byte[conn.getInputStream().available()];
conn.getInputStream().read(response);
System.out.println("Response: " + new String(response));
My command just gets echoed back:
Response: DELE test1.png
I think you need to do the retrieve and delete in two separate operations; some random documentation I found for FtpURLConnection says, in part:
This class Opens an FTP input (or output) stream given a URL. It works as a one shot FTP transfer :
Login
Get (or Put) the file
Disconnect
I did not see any methods in the documentation that would allow deleting a file.
You may wish to use the URL mechanism to retrieve the file, but I would drop down to using raw sockets to delete the file. Create a new connection to the FTP command port, log in, and issue the DELE command manually. If this is the only step you're taking, you might be able to get away with doing relatively poor error handling and maybe only two read() requests and simply show the output transcript to the user once you're done.
It's a bit dirty, but I completely understand not wanting to carry around a megabyte of additional source to achieve the moral equivalent of echo -e "user foo\npass password\ndele /path/to/file\nlogout" | nc ftp.remote.example.com 21.
Can you use some FTP clients to do the operation?
You can try http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html from the Apache commons-net. It is easy to use.

Categories