I'm not sure if this question has been asked before, but I could not find any resources on the internet answering my specific problem.
I am trying to upload a file from an Android app to my Openshift server/gear, where it will be stored. However, the issue I am facing is that whilst the file is being created at the Openshift side (I have checked using FTP), no data is being written to it.
The code snippet from the servlet that writes the data to the file is here:
int BUFFER_LENGTH = 4096;
DataInputStream din = new DataInputStream(req.getInputStream());
String fileName = din.readUTF();
String path = System.getenv("OPENSHIFT_DATA_DIR") + "/uploads/" + fileName + ".txt";
File f = new File(path);
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[BUFFER_LENGTH];
int length = 0;
while ((length = din.read(buffer, 0, BUFFER_LENGTH)) != -1) {
fos.write(buffer, 0, length);
}
fos.close();
din.close();
It all seems to be correct, to me at least, and it worked when I tested it on a local tomcat server. For some reason, however, it doesn't work with Openshift, so there must be something I am missing.
Luckily there is a help center article for just this issue:
https://forums.openshift.com/how-to-upload-and-serve-files-using-java-servlets-on-openshift
It details the code for both uploading, and serving files on OpenShift via a Java Servlet, using the openshift data directory
Related
Our application is deployed on weblogic server 12c. This application needs to copy files from server to some folder on the network location.
How can this be achieved in Java?
Application code is like
String source =
"C:\Oracle\Middleware\Oracle_Home\user_projects\domains
\base_domain\pdf_files\ABC.pdf";//Location on server
String destination = "\\machineA\SharedFolder";//shared folder in some machine on same network
FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(destination);
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Getting a error message
java.io.FileNotFoundException: \\machineA\SharedFolder\ABC.pdf (Access is denied)
machineA(server) can be added in the shared folder's Sharing options as machineA$.
Then this code which is running from application server will be able to access the location.
Reference: https://serverfault.com/questions/135867/how-to-grant-network-access-to-localsystem-account
I am currently writing a web service that stores an image on a directory on my glassfish server instance. For some reason, when the web service is consumed from the client, it says that directory or the file is not found. I am passing a byte array to the method. Below is my code:
#WebMethod(operationName="upload")
public String upload(String fileName, byte[] imageBytes){
String filePath = "/bucketlister/images/feature pictures/"+fileName;
try{
FileOutputStream fos = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(imageBytes);
bos.close();
return "Received file:" + filePath;
}catch(IOException e){
throw new WebServiceException(e);
}
}
the FileOutputStream throws a FileNotFoundException. Any ideas? I would prefer to use a byte array, but I am open to other suggestions.
I guess the answer was staring at me all the while.. The filePath was pointing to only half the directory. It should have gone
String filePath ="home/user/glassfish4/domains/domain1/bucketlister/images/feature_picture/" + fileName;
I don't know if it changes anything, but I changed the "feature picture" directory to "feature_picture"
I have written a servlet which will download the file from a server location. In our own INTRAnet the download seems to be very very slow and also when I have the Adobe addon installed in my browser and if I am downloading a PDF file, the Adobe addon will display the progress bar while downloading the PDF but this is not happening in my case! Below is my code! Should I not response it as attachment?
PrintWriter out = response.getWriter();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setIntHeader("Refresh", 1);
response.setHeader("Content-Disposition",
"inline; filename=\"" + fileNameWithExtension
+ "\"");
FileInputStream fileInStream = new FileInputStream(
filePathWithExtension);
BufferedInputStream bufferInStream = new BufferedInputStream(
fileInStream);
int cnt;
while ((cnt = bufferInStream.read()) != -1) {
out.write(cnt);
}
fileInStream.close();
out.close();
Not sure if there is a better way to do. Basically I tried converting one of my dot net code into this Java Servlet. THe current .NET code is very fast compared to this!
This is hosted on Apache Tomcat and the .NET code is hosted on IIS.
Reading and writing a byte at a time is horrifically inefficient. The canonical way to copy a stream in Java is as follows:
byte[] buffer = new byte[8192]; // or more if you like
int count;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
And you should not use a Writer here, use an OutputStream.
I am trying to download a file from a given URL. The URL is not a direct file URL. When this URL is provided in browser manually, we get a prompt for download/save.
For example, http://www.my-domain.com/download/type/salary/format/excel
I have no issues in the URL which has the file name directly in the URL. In the above URL, based on the format and type, server generates the file.
In Java I am trying to download the file using the below code. The file is created, but the content is just the domain content and not the actual excel data.
URL url = new URL("http://www.my-domain.com/download/type/salary/format/excel");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
float totalDataRead = 0;
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
FileOutputStream fos = new FileOutputStream("c:\\test.xls");
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int i = 0;
while ((i = in.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
bout.write(data, 0, i);
}
bout.close();
in.close();
The content is whatever the server sent for that URL. You can't do anything about that from the client end. If it contained Javascript for example it won't get executed.
When you want to solve a problem you have to use the adequate tools to get the thing done. The adequate tools can be found at poi.apache.org. Have a look at Apache POI.
My web application is written using jsp/javascripts. Backend Java. Have managed to implement the code to save an image outside webapplication, because I dont want to save the images in webapp/images folder(because when the server is down and when rebuild the app, I lose those saved images). What I want is to access those images I saved in my local directory again from my web app but I dont know how to. How can I access my local folder from jetty server, and jetty server is running on the same local machine...
Get the path to /image to store & read files by
getServletContext().getRealPath("/images");
Even you can read the file from external location from your servlet
File image = new File("d:\\image\1.jpg");
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(image.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open streams.
input = new BufferedInputStream(new FileInputStream(image), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
// Gently close streams.
close(output);
close(input);
}
Also See
Image Servlet