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.
Related
I am creating a micro service where my application will go to Apache Server to get files and folder-contents.
Not all the time, but very frequently, my Apache Server just spins when I try to see if file/folder exists:
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
or when I try to open connection:
c = (HttpURLConnection)url.openConnection();
And I end up restarting service.
I can clearly access it by entering URL manually and get a 200 response code.
I configured Apache to allow Index view for these files and folders.
Sometimes, when I type full path to a file using IP address, it spins also.... I find this very strange.
I don't know why this would happen... Any ideas?
Version: 2.4.23 win64 VC14
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.
https://saucelabs.com/resources/selenium-file-upload
I am trying to follow the above sample, but I would like to pass the input image file from a HTTP server instead of the local disk. How should I do it? I tried with the HTTP url but it doesn't seem to work as an argument.
You can't directly pass a file that is not on your local drive.
but you can do it in 2 steps:
1 - download the file. ex with http://commons.apache.org/proper/commons-io/:
FileUtils.copyURLToFile(aRemoteUrl, aLocalFile)
2 - upload the local file (as explained in your link)
(you can eventually delete the local file after that)
I am working on an application in which users are allowed to upload Image files. I am follow ing the approach of saving the file in a directory and saving its name in database for retrieval.
inputStream = file.getInputStream();
BufferedImage imBuff = ImageIO.read(inputStream);
imBuff = Scalr.resize(imBuff, 200);
File dir = new File("/opt/uploads/contactImages");
if(!dir.exists()){
dir.mkdirs();
}
ImageIO.write(imBuff, "png", new File("/opt/uploads/contactImages"));
Hence with the above code my files are getting uploaded in opt/uploads/contactImages folder and is working fine in my local environment.
But in production I have hosted my war on openshift server. The images are not getting created there, may be because I dont have the root access to create such directory? What should be the possible approach to solve this issue?
As far as I know we all are using instance of App/Web server on openshift, we dont have standalone servers here so we wont be having write access to server, instead try uploading BLOB object in database from which you can retrieve it easily
Using openshift variable
System.getenv("OPENSHIFT_DATA_DIR") uploads the file in app-root/data directory
Reference
https://www.openshift.com/forums/openshift/how-to-upload-and-serve-files-using-java-servlets-on-openshift
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.