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
Related
I am working in a jsp file. I have a lot of files available in my Sharepoint intranet with link like this :
http://myIntranet.eu/my%pdf%1.pdf
I want saved the pdf in a folder in my server. This will help me then to merge all PDF.
I had already merge pdf in java, but I don't know how I can save this PDF on my server.
Can you please help me on how to do that.
If there is no web service or rest service from sharepoint to get the pdf, you can try to use an http client http://hc.apache.org/httpcomponents-client-ga/ to make request to sharpoint on java server to get the pdf file. Maybe you need authentication.
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.
I'm working on a play framework 2 application and I would like to call a WebService and send a file ( an image ). I found the WS class but I cannot find how to send a file using it.
What I found is this :
WS.url("http://localhost:9001/post").post("content")
But I did not managed to send a file using a POST request.
Can someone tell me how to do it ?
Thanks.
C.C.
With Play > 2.0 this should do the trick:
File file = new File("yourPath");
WS.url("/post/url").post(file);
Being able to add a parameter identifying your file the request should be send using multipart/form-data.
This Post shows how to do it with Play! - https://stackoverflow.com/a/18723326/2788883
use following method to return response as file
RenderBinary(java.io.File file, java.lang.String name)
To see API follow this link.
I want to send a xml file through POST request to a HTTPS server.
I think i have to read the content of the XML file and save it to a String, then send POST it to the server.
Sorry, but i don't have any code to demonstrate my workings. Can someone help me to do a Sample code or tutorial to start with this task ?
Note : I am using Java
Using java, You can open this XML file in READ mode and start reading and printing contents in browser.
I am writing a Java client that needs to upload a file to a server using a REST post. I have a schema for information that is to be sent along with the file, but the file itself is to be sent as an Attachment to the message. The server is not written in Java and I don't have (easy) access to the source.
How do I go about creating the post message in CXF? I found a similar SO question but it seems to use Jersey-specific classes that I can't find in CXF. CXF is already being used in the project so I'd prefer to use it, but I could use another library if required.
In case it's not obvious this is the first time I've worked with a REST service.
Did you see the bit in the Apache CXF User Guide wherein WebClient is used with MultipartBody, Attachment or File? The example code excerpt is shamelessly copied below:
WebClient client = WebClient.create("http://books");
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg");
Attachment att = new Attachment("root", imageInputStream, cd);
client.post(new MultipartBody(att));
// or just post the attachment if it's a single part request only
client.post(att);
// or just use a file
client.post(getClass().getResource("image.png").getFile());