I am trying to upload file to servlet. I am trying to add file name to header and read in on servlet... But in the servlet side the file name (containing cyrillic) I can get is only ??? ?????.wmv. So my question is how to upload file with ciryllic file names correctly?
I use HttpClient 4
the code snippet:
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.
PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
String url="testUrl";
httppost = new HttpPost(url);
httppost.addHeader(FILE_NAME_HEADER, file.getName());
Any useful comment is appreciated :)
Andrew
Personally I'm using Apache Commons FileUpload library to handle file uploads. Here is the link. Instead of doing Your own, you can use this library. If it doesn't satisfy your needs, you can still work on your own solution.
Use HttpMultipartMode.BROWSER_COMPATIBLE mode.
See https://issues.apache.org/jira/browse/HTTPCLIENT-293 for the details
Related
Using URL class in java.net package.
Method 1
String sourceUrl = "https://thumbor.thedailymeal.com/P09kUdGYdBReFSJne1qjVDIphDM=//https://videodam-assets.thedailymeal.com/filestore/5/3/0/2_37ec80e4c368169/5302scr_43fcce37a98877f.jpg%3Fv=2020-03-16+21%3A06%3A42&version=0";
java.net.URL url = new URL(sourceUrl);
InputStream inputStream = url.openStream();
Files.copy(inputStream, Paths.get("/Users/test/rr.png"), StandardCopyOption.REPLACE_EXISTING);
Using Apache's HttpClient class.
Method 2
String sourceUrl = "https://thumbor.thedailymeal.com/P09kUdGYdBReFSJne1qjVDIphDM=//https://videodam-assets.thedailymeal.com/filestore/5/3/0/2_37ec80e4c368169/5302scr_43fcce37a98877f.jpg%3Fv=2020-03-16+21%3A06%3A42&version=0";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(sourceUrl);
HttpResponse httpresponse = httpclient.execute(httpget);
InputStream inputStream = httpresponse.getEntity().getContent();
Files.copy(inputStream, Paths.get("/Users/test/rr.png"), StandardCopyOption.REPLACE_EXISTING);
I have downloaded the rr.png file using both the methods. I found both the files are different even in sizes also and using method 2 download a blank image. I have read both the methods are same but I do not understand why method1 downloading correct file and method2 downloading wrong file. Please clarify this and also let me know if there is a fix in the method 2 through which I can download the correct file.
First: cross-posting: https://coderanch.com/t/728266/java/URL-openStream-HttpResponse-getEntity-getContent
Second: I guess the issue is the url and how it's handled differently by javas internal class and apache lib - use a debugger and step through them to see what url really gets send out the tls stream.
I am wanting to upload a video file (.mp4) by POST Request to JIRA. The file gets uploaded to the server, but the video becomes corrupt (i.e. opening it doesn't work). Sending other attachments, like screenshots (.png) and text files (.txt), works fine without corrupting the file.
I am using the Apache HttpComponents HttpClient 4.3.6.
Here is example code:
File file = new File("location/to/file.mp4");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create().addBinaryBody("file", file);
HttpPost postRequest = new HttpPost();
postRequest.addHeader(HttpHeaders.AUTHORIZATION, BASIC_AUTH);
postRequest.addHeader("X-Atlassian-Token", "nocheck");
postRequest.setEntity(multipartEntity.build());
postRequest.setURI(uri);
CloseableHttpClient client = HttpClients.createDefault();
try {
HttpResponse response = client.execute(request);
} finally {
client.close();
}
I attempted to add a video/mp4 MIME type but that didn't seem to help any:
MultipartEntityBuilder.create().addBinaryBody("file", file, ContentType.create("video/mp4"), file.getName())
The issue I had here was that QuickTime on Mac wasn't compatible with the .mp4 file format. I downloaded VLC media player and the file worked just fine without specifying a MIME type.
i am creating a desktop application which send file to an tomcat server. the servlet receiver and saves file fine.
I need some help to do a java program that post in a https site. I dont know how to put the parameters because it a multpart form data contect type.. Please help! when I do a post with firefox its like this...
This will depend. I've used the following technique to upload a multi-part file to a server before, based on providing a series of form key/name pairs.
This will be depended on you own requirements and what the servlet is actually expecting...
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String name = file.getName();
entity.addPart(new FormBodyPart("someFormParameter", new StringBody("someFormName")));
/*...*/
entity.addPart("formFileNameParameter", new FileBody(file, mimeType));
HttpClient client = /*...*/
HttpPost post = new HttpPost(url.toURI());
post.setEntity(entity);
HttpResponse response = client.execute(post);
// Process response
Right now I am using Httppost to Post some parameters in the form of xml to a server. When the post occurs, a geotiff or .tif file is downloaded. I have successfully posted the document to the server and successfully downloaded the file simply by attaching the parameters to the url but I can't seem to combine the two. I have to use post because just using the URL leaves out elevation data in the geotiff.
In short, I am not sure how to simultaneously post and retrieve the image of the post. This is what I have thus far...
// Get target URL
String strURL = POST;
// Get file to be posted
String strXMLFilename = XML_PATH;
File input = new File(strXMLFilename);
// Prepare HTTP post
HttpPost post = new HttpPost(strURL);
post.setEntity(new InputStreamEntity(
new FileInputStream(input), input.length()));
// Specify content type and encoding
post.setHeader(
"Content-type", "text/xml");
// Get HTTP client
HttpClient httpclient = new DefaultHttpClient();
//Locate file to store data in
FileEntity entity = new FileEntity(newTiffFile, ContentType.create("image/geotiff"));
post.setEntity(entity);
// Execute request
try {
System.out.println("Connecting to Metoc site...\n");
HttpResponse result = httpclient.execute(post);
I was under the impression that the entity would contain the resulting image. Any help is much appreciated!
Thanks for the help guys. The entity was what was being sent to the server. I had code that was trying to read it from the response as well but it wasn't working because setting the entity to a file entity messed up the post request. By removing that part, it works great!
I have an enormous file to upload and server on other side does support chunked upload.
Is there any example of how exactly to do that?
Or there is some other library which do that?
Using HttpClient 4 (From Apache)
HttpPost post = new HttpPost(url);
MultipartEntity content = new MultipartEntity(HttpMultipartMode.STRICT);
//To add parameters, use StringBody
content.addPart("param", new StringBody("value"));
content.addPart("param1", new StringBody("value1"));
content.addPart("param2", new StringBody("value2"));
//To add files, use InputStreamBody
content.addPart("source", new InputStreamBody(inputStream, mimeType, fileName)); //OR
content.addPart("source", new InputStreamBody(inputStream, mimeType));
//Finally
post.setEntity(content);
Hope this helps.
Using HttpURLConnection, just set chunked transfer mode.