7

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.

4
  • 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. Commented May 23, 2012 at 13:13
  • 1
    MAYBE the question should be made more clear. Don't expect accurate answers with a question this fuzzy! Commented 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. Commented 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. Commented Jan 27, 2018 at 18:46

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Specific folder on remote server. my application run on my server, file host isn't where my app run.
As i said, the remote server/file host must provide the API for specifying the remote destination when you upload via HTTP
3

Have a look at apache commons-fileupload. You can find sample code here.

Comments

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); 

3 Comments

where should I add my remote server url? In your code, what is "a.txt"? Can you explain more for me?
add ur server url in YOUR_SERVER_URL...a.txt is a file which i sent 2 server
I used your code, but nothing moved and no exception throws. Can you guide me? I changed nothing in your above code. Thanks
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.

Comments

0

You can use HttpClient.

Send the files using POST as a method.

Comments

-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();

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.