Uploading Image path in Linux Hosting Server (Java) - java

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

Related

Upload a file to an SFTP server using PDFBox save method without storing the file to the local system?

I'm trying to save the edited PDF which I fetched from the remote server back to its location without having it downloaded/stored on the local machine. I'm using JSch SFTP method to get the input PDF file from the SFTP server using
x = new BufferedInputStream(channelSftp.get("example.pdf"));
PDDocument document = PDDocument.load(x);
and after doing some edits using PDFbox, I'm trying to save it using:
documents.save(new File("ftp/path/location"));
I am not able to because I know it only works for your local directory only. Also I can see that document.save accept OutputStream` parameter, but I do not know how to use it here.
I don't have any problems with taking input using stream reader.
All I need is to save that edited PDF back to its location (possibly replace) without having to download it on my local system.
Use the ChannelSftp.put overload that returns OutputStream:
try (OutputStream out = channelSftp.put("example.pdf")) {
documents.save(out);
}

Not able to use thumbnail link of a google drive file in web application java

I'm working with drive v2 API to upload files to google drive using Oauth2.0. This is how I get the thumbnail link of the file.
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setParents(Arrays.asList(new ParentReference().setId(parentFolderId)));
fileMetadata.setTitle(file.getName());
fileMetadata.setMimeType(type);
java.io.File filePath = new java.io.File(file.getAbsolutePath());
FileContent mediaContent = new FileContent(type, filePath);
com.google.api.services.drive.model.File f = service.files().insert(fileMetadata, mediaContent).execute();
String thumbnailLink = f.getThumbnailLink()
I'm using this thumbnailLink to show images in my web application. Does this thumbnailLink will have validity? Because when i tried to use the thumbnail link after sometime i got an 403 error.
Your client does not have permission to get URL
/Bvz7wBXoC_pXJWfbJ44iEryxrXxB5w7JLU9He8ErJ2OjWEZg2EJCqBL1ElfIUdVx--IHqw=s220 from this server. (Client IP address: x.x.x.x)
I have tried with these Google Drive - Unable to download thumbnails with an authenticated (OAuth2) request , https://productforums.google.com/forum/#!topic/chromebook-central/YvNs0J5IgS0
Still I'm facing the same issue. Is there any way I can overcome this? When i upload the same image again it works for sometime but after sometime again the same issue.
thumbnailLink according to docs has
A short-lived link to the file's thumbnail, if available. Typically
lasts on the order of hours. Only populated when the requesting app
can access the file's content.
So it was only working as indicated in the docs.

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.

How to pass image URL from from a HTTP server in selenium

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)

Categories

Resources