I need to upload images and txt files from my application to a remote server (Just http no ftp) using java. My application is in jsf framework. I searched but no suitable things found. Can anybody guide me? In fact I should upload files to special folder to remote server. I have two application with shared path to upload files, so for accessing them to this files, I decidec to upload shared files(such as images and texts) to third server. First application should upload files to this remote server and second application should read them from it. So my hard part of this solution is to upload files to this third server(in fact remote server) using http.
-
Everyone who suggests commons fileupload to send the file is wrong. It's intented to parse a multipart/form-data request on the other side, not to create and send multipart/form-data request on your side.BalusC– BalusC2012-05-23 13:13:24 +00:00Commented May 23, 2012 at 13:13
-
1MAYBE the question should be made more clear. Don't expect accurate answers with a question this fuzzy!Alexis Dufrenoy– Alexis Dufrenoy2012-05-24 08:08:02 +00:00Commented May 24, 2012 at 8:08
-
I have two application with shared path to upload files, so for accessing them to this files, I decidec to upload shared files(such as images and texts) to third server. First application should upload files to this remote server and second application should read them from it. So my hard part of this solution is to upload files to this third server(in fact remote server) using http.zorro6064– zorro60642012-06-02 05:54:39 +00:00Commented Jun 2, 2012 at 5:54
-
First consider following: 1. Is an http server running on your "third server"? 2. Can you upload custom code over there? If answer to first question is yes then answers provided below will work. Else first you have to install either tomcat,glassfish or some other http server overthere then work your way.rt.jar– rt.jar2018-01-27 18:46:01 +00:00Commented Jan 27, 2018 at 18:46
Add a comment
|
6 Answers
To upload file to a specific folder, your server API must support that.
Server side for receiving uploaded files, you can use http://commons.apache.org/fileupload/
Client side for sending a file upload request, you can use https://hc.apache.org/httpcomponents-client-ga/index.html
Use following code:
byte[] data = bos.toByteArray();//convert ur file into byte[]
HttpClient httpClient = new DefaultHttpClient();//Client
HttpPost postRequest = new HttpPost(YOUR_SERVER_URL);//Post Request to specified URL
ByteArrayBody bab = new ByteArrayBody(data, "a.txt");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);// Multipart data
reqEntity.addPart("uploadingFile", bab); //adding data to request entity
postRequest.setEntity(reqEntity);//adding request entity to post request
HttpResponse response = httpClient.execute(postRequest);
3 Comments
zorro6064
where should I add my remote server url? In your code, what is "a.txt"? Can you explain more for me?
P Srinivas Goud
add ur server url in YOUR_SERVER_URL...a.txt is a file which i sent 2 server
zorro6064
I used your code, but nothing moved and no exception throws. Can you guide me? I changed nothing in your above code. Thanks
As per your requirement, you need to send multiple images and text files. So HTTP multi-part file upload seems to be a suitable solution. You can get further information on this from here.
Comments
make
@Autowired
ServletContext c;
or take object
byte[] bytes = file.getBytes();
String UPLOAD_FOLDEdR=c.getRealPath("/images");
Path path = Paths.get(UPLOAD_FOLDEdR +"/"+ file.getOriginalFilename());
Files.write(path, bytes);
System.out.println(path);
String Pic_Name =file.getOriginalFilename();
1 Comment
melwil
Could you please edit your answer so it makes sense? Currently the code is hard to read, and it's not really clear how this solves the issue in question. Please explain your answer better.