I'm working on converting a very simple java desktop application to run in java web start and I'm having all kinds of trouble with the input/output files. Most specifically I can't seem to find any information on how to handle i/o in a web start application. I tried placing the input files in the same folder on my web server as the jar and jnlp file, but it doesn't read it.
I've got one input file that I want to keep on the web server and read into the application from there.
I've got a second file that I want the application to generate on the client machine the first time it's run, and read in from there every time thereafter.
If anyone knows what considerations I need to take for i/o in java web start or can point me towards a resource that explains it I would appreciate it.
You can find out where you were downloaded from with BasicService and then use HTTP to transfer the file. You could also just add them to a jar.
You can store a limited amount of information (I think it currently defaults to 128K/muffin) with PeristenceService.
http://java.sun.com/javase/6/docs/jre/api/javaws/jnlp/
You don't have many choices.
You
could read the file from http new URL(address).openStream()
You could embed the file in the jar
which I believe you don't want to,
and then use getResourceAsStream()
You usually store files on the user hard drive using a hidden folder
public final class ApplicationConstants{
final static String HOMEDIR_STRING = System.getProperty("user.home");
final static File HOMEDIR = new File(HOMEDIR_STRING);
final static File CONFIG_DIR = new File(HOMEDIR, ".com.mycompany.myapp");
}
///
if(!ApplicationConstants.CONFIG_DIR.exists()) ApplicationConstants.CONFIG_DIR.mkdirs();
File outputFile = new File(ApplicationConstants.CONFIG_DIR, "my.xx"));
Related
How can I auto launch JNLP file "Programmatically".
I have been able to auto-download the JNLP file but then I have to click on the downloaded file to run it. I'm aware that I can 'open' it every time instead of 'saving' it and remembering this choice. But this is not what I want, I cannot tell client to open the file every time.
Can this be done programmatically?
I suspect that this(opening JNLP instead of saving, programmatically) can not be done, but I absolutely have no idea, any help would be appreciated.
Yes launching a JNLP file programmatically can be done, see below code is using Apache common io to copy streams (Line 3) but there are different ways to copy streams:
final File jnlp = File.createTempFile("temp", ".jnlp");
final URL url = new URL("http://your_jnlp_file_url");
IOUtils.copy(url.openStream(), new FileOutputStream(jnlp));
Desktop.getDesktop().open(jnlp);
I have based my code from below stack overflow question where one of the answers show how to call JNLP URL programmatically:
Combination of Launch4J and Java Web Start?
When a user clicks a download button (coded in simple Javascript) on my webpage, it triggers a PHP function that calls to a java file. This Java code connects to the database and writes to a text file.
Currently, the code is in development on my local machine. Here is a snippet of the code is currently creating a local text file and writing the information to it:
StreamFactory sf = StreamFactory.newInstance();
sf.loadResource("mapping.xml");
File file = new File("C:\\MyLocal\\foo.txt");
BeanWriter bw = sf.createWriter("fileExport", file);
// writes beans
bw.write("", "");
...
bw.flush(); // flushes to foo.txt located in C:\MyLocal
My question is is it possible to write this dynamically created text file to the user's computer instead of my local, and if so, is it a good web development practice? The benefit this way is that I don't need to store foo.txt on the server that the code will reside. However, I'm not very familiar with web development practices, and did not see a concrete rule on this subject matter.
If it is better to save it to the server and then implement some download code from there, should this be handled within the PHP of the page or the Java backend functions?
It's not possible to write on user's computer filesystem.
Because of javascript security measures, if it wasn't because of this, a malicious website/webapp could write a huge amount of data on users hard drive, or could write malicious scripts.
The option a user has it's to download a file, but a webpage doesn't have direct access to user filesystem.
There was a proposal API for doing something like this, but it was discontinued
https://www.html5rocks.com/en/tutorials/file/filesystem/
Answering to your question: definitely it's not a good practice to try to write files on users filesystem
You should handle the download with Java on the Backend
I want to download a file from one of the EBS volumes I created on Amazon Elastic block storage. Mostly it is advisable to used ServletContext#getResource() and its counterpart ServletContext#getResourceAsStream() as well advised here.
But in this case is the following code advisable
InputStream in = new FileInputStream(new File(FOLDER_PATH_ON_AMAZON_EBS + "/" + folder + "/" + fileName));
It's hard to tell what the question is here.
If you are asking whether it is better to use getServletContext() or new File(PATH_TO_EBS...) then it simply depends on what you are running. If you are running a standalone java application and requesting files via sockets, then you would use the latter (a FileInputStream over a file you know where to look). If you are running a web server (eg Tomcat) and will be using a web client to download the file, then you would typically use the getServletContext() since that is part of the web-server infrastructure.
Both ways let you get a handle on the file, but getServletContext() is going to refer to a location for your application under Tomcat's working area. Are you going to mount your EBS volume somewhere where you can easily reach it starting from Tomcat's working area.
If you are running a web server and it is allowing you to reach a file directly in your EBS volume with new FileInputStream(new File(MY_EBS_LOCATION + "/" + ...)) then use it by all means - clear and easy.
Having the file path in the properties file, and using the absolute path is better due the following reasons,
There are chances that the mountpoint of the ec2 volume changes, having that in the classpath and modifying that is comparatively harder than modifying the entry in the properties file.
Normally resources like property files are got using getResource(), if there is going to be other IO like storing photos, office docs then its wise to use absolute path for access.
It also depends on the usecase if the files(resource) is frequently used by the application, and the application only, then u can have that in classpath and pack it along with the archive.
This question already has answers here:
How to save uploaded file in JSF
(2 answers)
Closed 7 years ago.
Here with another question on Images ( which seems to be more difficult than I initialy predicted) I'm working on a java Web app with JSF 2.0 ( apache myFaces) and I want this app to be able to upload a picture to a destination on the server it's going to run on. I have a Windows r2 2008 Server running a mySQL Db, but I don't want to store the image in the db, I'd rather store it somewhere in the server and then just save the path as a string in the db.
I was told this is the best way, but I can't seem to find an example on how to save it on the server. I run the app on the Apache tomcat Server as a WAR file. so I don't know if I have to save the file to a path on the server drive (i.e. C:\images) or a special folder in the project itself ( within the java, html files) any help at all is greatly appreciated. I'm totally lost and have been stuck the whole day trying to figure this out.
The code I use to upload the image to the java class is this ( courtesy of CodyS):
InputStream is = uploadedFile.getInputStream();
byte[] buffer = new byte[(int) uploadedFile.getSize()];
is.read(buffer);
File f = new File("C:\\temp\\" + this.patient.getPk() + ".jpeg");
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer); //This is where I write it to the C Drive
fos.close();
is.close();
instead of writing it to my C drive I'm going to run it on the server, but where should I store the image to later retriev and display in an xhtml file? I hope I'm being clear on what I need, let me know if I am not and I'll try to explain in another way.
instead of writing it to my C drive I'm going to run it on the server, but where should I store the image to later retriev and display in an xhtml file?
That depends on how much control you have over configuring the server. Ideal would be to configure a fixed path outside the Tomcat webapps folder. For example, /var/webapp/upload. You can set this path as a VM argument or environment variable so that your webapp can retrieve it programmatically without the need to change the code.
For example, when specifying as VM argument -Dupload.location=/var/webapp/upload, you can complete the upload as follows:
Path folder = Paths.get(System.getProperty("upload.location"));
String filename = FilenameUtils.getBaseName(uploadedFile.getName());
String extension = FilenameUtils.getExtension(uploadedFile.getName());
Path file = Files.createTempFile(folder, filename + "-", "." + extension);
try (InputStream input = uploadedFile.getInputStream()) {
Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
}
String uploadedFileName = file.getFileName().toString();
// Now store it in DB.
As to serving the file back, most ideal would be to add the upload location as a separate <Context> to Tomcat. E.g.
<Context docBase="/var/webapp/upload" path="/uploads" />
This way you can access it directly by http://example.com/uploads/foo-123456.ext
If you have zero control over configuring the server, then, well, storing in the DB or sending to a 3rd party host such as Amazon S3 is your best bet.
See also:
How to provide relative path in File class to upload any file?
Reliable data serving
I would consider allowing the user to upload to Amazon S3 directly. Amazon offers a service for that. Using that service, the client would post a form with the file directly to S3. Once the file has arrived there, Amazon will redirect the client to one of your endpoints, to confirm that the data has arrived, passing you the relevant details.
The benefits are:
Your server does not spend a lot of time in receiving huge files. You can spend your CPU cycles on something a little bit more interesting.
The availability guaranteed by storing it on S3 is probably better then what you would get by storing it on your own Windows box.
It scales. At some point, your filesystem will run out of space. (Or you reach the limit of what you can store inside a folder.)
I'd suggest that you save your images in a subfolder which is in your application's WEB-INF folder. Remember that when you use Tomcat, your WAR files will be extracted automatically. This approach also has the advantage that you can always migrate your application to another server, you only have to save the path relative to WEB-INF folder in your DB.
I'm new to Java. I'm simply trying to build a .jar file of my applet so I can run it from my browser. This is what my directory structure looks like:
C:\java\pacman\src
contains all of the .java class files.
C:\java\pacman\assets
contains about 4-5 images and audio files.
If I try to use the following code:
Image someFile=getCodeBase().toString() + "file.png";
The result of getCodeBase() is
file:/C:/java/pacman/bin/
However the following code fails to load:
img=new ImgHelper(getCodeBase().toString() + "assets/");
ImageIO.read(new File(img.getPath("pacman.png")));
Moving my 'assets' folder to the 'bin' folder didn't fix this either. It tries loading:
file:/C:/java/pacman/bin/assets/pacman.png
saying:
Can't read input file!
But the url it gave opens fine if I paste it into run and hit enter:
So to avoid myself a lot of headache i commented out the code in my ImgHelper class and did this:
public ImgHelper(String dir)
{
//this.imgDir=dir;
imgDir="C:\\java\\pacman\\assets\\";
}
Which works perfectly. But I want to put this on a web server, and I have no idea how/what I should do to make all the images and sounds work. Any ideas?
Thanks...
Why not put it all in a JAR file and then call Class.getResourceAsStream?
A JAR file is better as it is a single HTTP connection rather than one HTTP connection per file. It is also much more flexible to use a Stream than a File.
getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.
EDIT:
Another thing, the File method won't work if the applet is on a server as it will be trying to open the file from the local machine (I think, I haven't tried it) rather then from the server. Even if it tried to create a file path to the server that won't work.
I agree with tofubeer about the JAR, but if you want to put the image on your server, see the tutorial on Applet images here. The codebase will be whatever location your applet is on the server, and you can put images relative to that on the server as well. Use a media tracker along with the Applet.getImage() method to retrive the url. From the example:
my_gif = getImage(getDocumentBase(),"imageExample.gif");
There are two possible solutions that would work:
The images could be present outside the applet JAR. The applet could then be initialized with the location of the directory where the images are present. Once you have that information you could then load images from the server. The Sun Java tutorial provides an example usage of the applet parameter to pass the image source directory.
The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.
PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application. The same section discusses a lot of the points brought forth by TofuBeer and John.
EDIT : The usage of the File API is not recommended because it ends up reading off the local file system. That is unacceptable for most users on the internet.