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.
Asked
Active
Viewed 5.4k times
7
-
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 May 23 '12 at 13:13
-
1MAYBE the question should be made more clear. Don't expect accurate answers with a question this fuzzy! – Alexis Dufrenoy May 24 '12 at 08: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 Jun 02 '12 at 05: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 Jan 27 '18 at 18:46
6 Answers
6
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

gigadot
- 8,879
- 7
- 35
- 51
-
Specific folder on remote server. my application run on my server, file host isn't where my app run. – zorro6064 May 23 '12 at 11:57
-
As i said, the remote server/file host must provide the API for specifying the remote destination when you upload via HTTP – gigadot May 23 '12 at 12:01
2
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);

P Srinivas Goud
- 886
- 1
- 12
- 30
-
where should I add my remote server url? In your code, what is "a.txt"? Can you explain more for me? – zorro6064 May 26 '12 at 06:31
-
add ur server url in YOUR_SERVER_URL...a.txt is a file which i sent 2 server – P Srinivas Goud May 27 '12 at 09:38
-
I used your code, but nothing moved and no exception throws. Can you guide me? I changed nothing in your above code. Thanks – zorro6064 May 30 '12 at 11:41
0
You can use HttpClient.
Send the files using POST
as a method.

Ihor Patsian
- 1,288
- 2
- 15
- 25

Subir Kumar Sao
- 8,171
- 3
- 26
- 47
0
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.

Ihor Patsian
- 1,288
- 2
- 15
- 25

user1157934
- 41
- 1
- 8
-4
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();
-
1Could 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. – melwil Jan 27 '18 at 22:15