Remote file retrieval - java

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.

Related

ImageIO.read( new URL(imagePath)) is not responding for HTTPS URLS

My code is working as expected on localhost starter kit but when I deployed it to development server my below code is taking around 2-3 mins and after that it says
Invalid URL
The requested URL "http://%5bNo%20Host%5d/index.html?", is invalid.
Reference #9.3d7c4117.1550338465.3d54af04
My code is as follows:
imagePath = "https://i.imgur.com/TrNzuMY.jpg";
Image image = null;
image = ImageIO.read( new URL(imagePath))
Is this something AEM on development server is not allowing to access external URLs?
This is AEM 6.3 and this code is called in a JSP which is getting called by another JSP using AJAX GET request.
Edit (After Sumanta Pakira response): This is only happening when passed URL is Secure i.e. HTTPS, for HTTP URLs it is working as expected.
There are two solutions :
Add the server (i.imgur.com) certificate into your AEM server trust store.
You can look at this example

SpringBoot cannot upload large files after using Nignx

SpringBoot uses the Nginx reverse proxy and then opens the HTTPS protocol. If the file transfer exceeds 500KB, the data in the Request will not be received.
You need to edit your nginx.conf and add this line into the server block:
server {
client_max_body_size 10M;
}
It means setting file size upload to 10M.

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.

How do I get url of uploaded file

I'm using apache.commons library for uploading file to a ftp server. It uploads file successful, but then I want to have a link to that file, that anyone can download it. How do I get the URL of uploaded file ?
Thanks.
One of the great features of an FTP server is the ability to use direct FTP links. With a direct link, you can use links to files and directories in emails and on your websites. Think of this: instead of sending a big attachment in an email, send a link to the file instead.
The format is really simple:
ftp:// username [colon] password [at] server.exavault.com [slash] folder-or-filename
For example: ftp://links:pass#test.exavault.com/transfer.pdf
source : http://www.exavault.com/docs/help/faq/transferring/direct-links

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

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

Categories

Resources