how to upload file to http remote server using java? [duplicate] - java

This question already has answers here:
How to use java.net.URLConnection to fire and handle HTTP requests
(12 answers)
Closed 5 years ago.
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.

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

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

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

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.

You can use HttpClient.
Send the files using POST as a method.

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

Related

How i can sent a zip directory to python server?

I am a rookie in android and now i encounter with such issue. I need sent a zip directory to the phyton server and the first i have tried make it with help of MultipartEntity, but it is use a HttpClient and HttpPost which don't support now with sdk 23 and depricate now.
And i figure out that i can use OkHttp instead. I found some samples, but they clear up how sent json file. There is loads of samples but no one how to sent a dir...
Who have a sample how to sent a zip directory, help me please...
Thanks!
try to use this:
HttpClient http = AndroidHttpClient.newInstance("MyApp");
HttpPost m = new HttpPost("http://url-to-your-python-server");
m.setEntity(new FileEntity(new File("path-to-zip-file"), "application/octet-stream"));
HttpResponse response = http.execute(m);

Accessing Remote files using HTTP from a web application

we have a requirement where all file (text , image , PDF etc) uploaded in the Server1. Now we have a Java web application running in a different server (Server 2) which has to read these files and show it in the GUI.
FTP is blocked in web application server. So i am thinking of using HTTP.
The files in Server1 is not distributed .i.e not inside any ear or war.
I managed to get the code to read a file from HTTP.
My question is
- How do i access this file through HTTP when these files are not distributed
- When i write a code accessing the file like http://server1:port1/location of file , its says file not found .
- how do i expose these files , so that i can access it via HTTP from my web application
That means, you want to download the content of the file.
Well at very first, open up a browser. Enter http://server1:port1/FILE_PATH .
what do you get as a response ?
Second, if you get "FILE NOT FOUND" then you need to configure a virtual path mapped to real path in server1. After that, try the URL from browser again. When it works,
URL website = new URL("http://server1/file_location");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("file_name");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
will help you to download the file to your local machine.

Remote file retrieval

In my web application (spring REST ) I have an API to upload the file to the server.I have another API which retrieve the file url to the client.
file = new File(fileName);
URL url = file.toURI().toURL();
InetAddress ip = InetAddress.getLocalHost();
String urls="file://"+ip+url.toString();
As a result I am getting file://192.168.3.37/D:/Anoop/pic/2unvvhlacq5fh09tokr7i25cvj.jpg as the url.
This works fine locally , When the application is hosted in a server url shows file not found.
Please advice.
Okay, Assuming that your server IP is 192.168.3.37 and your upload API will put the uploaded file into D:\Anoop\pic\ on the server. So what you have todo is to expose D:\Anoop\pic\ via protocols like HTTP or FTP.
Than you will have something like http://192.168.3.37/pics mapped to D:\Anoop\pic. This way, all file inside the directory were exposed using HTTP. You can do the same strategy with FTP.
So, what you return to the client is giving the URL to any speciffic file that was uploaded before, if you have uploaded the file 2unvvhlacq5fh09tokr7i25cvj.jpg and the server API put it on D:\Anoop\pic\2unvvhlacq5fh09tokr7i25cvj.jpg then it will be accessible with http://192.168.3.37/pics/2unvvhlacq5fh09tokr7i25cvj.jpg
I hope you get the idea.

xlsx file corrupted during downloading from server

Folks, here is the situation:
confirmed that the xlsx file is good on the disc.
I copied the file from server and can open without problems.
using FileInputStream and BufferedInputStream to handle the client side download function.
The download function i mean user can download the file by clicking a hyperlink, and a servelet call was made to the java class which uses FileInputStream and BufferedInputStream
Mime type was set correctly as application/vnd.openxmlformats-officedocument.spreadsheetml.shee
After download the file successfully, it will give a "converted failed" error while trying to use office 2003 to open this xlsx file.
any thoughts?
Thanks!
There are bytes which didn't belong in the HTTP response body or were simply missing there.
It's impossible to point out the actual root cause based on the information given as far. You have to check if the right bytes were written from local disk file system to the HTTP response body.

how to create a file and write on it on URL

HI can anybody tell me how to create a file and write on it on the URl.
Actually I am using an applet and from theat I want to create a file on
getCodebase(); so can any body tell me how can I do it
I have tried
URL url = new URL(/*url by codebase and the directory with file name*/);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStream out = connection.getOutputStream();
but it not worked
Please help me
Actually I am using an applet and from theat I want to create a file on getCodebase();
Well that wouldn't be very secure if you could do that, would it?
I think the route you want to take is to configure your HTTP server to handle PUT requests, and then form a PUT in Java. That is described here:
How to send PUT, DELETE HTTP request in HttpURLConnection?
However, your applet probably needs elevated permissions (in other words, a signed applet that operates outside of the normal sandbox). I'm not sure about that though; some connections are allowed back to the host so you might be OK.

Categories

Resources