I'm developing a java web app on my local PC that uses a file on the deployment server. I have to do something like this to get it to work both locally during development and on the server when deployed. Is this standard practice? Or is there a way in Eclipse using linked folders and files to specify the file the same as it is referenced on the server?
InputStream in = null;
try {
in = new FileInputStream("/EBWEB/www/homepages/path/to/file/STKCore.properties");
} catch (java.io.FileNotFoundException ex) {
in = new FileInputStream("W:/internal/www/homepages/path/to/file/STKCore.properties");
}
W: is how I have the remote server mapped using Samba in Windows XP on my PC.
You can put your properties file in your war and open it with a getResourceAsStream().
Related
My Springboot app is installed on one server and I've created a separate server (File Server) for images. Both servers are AWS EC2 instances.
So my goal is to write the image-files onto the File Server and anyone can read or access those files via HTTP protocol.
Apache HTTPD server is installed on the File Server. However, I cannot write the files on the File Server.
I'm getting "IOException/FileSystemException - Network path not found".
I've also installed FtpServer and I can upload/write files there. But then I cannot access the files from the internet using the HTTP protocol.
Code:
String path = generateFilePath(attachmentRequest); //--> //X.X.X.X/file-uploads/bfdec95a-d730-46b4-a835-d0f85a9bc0cc.jpg
File file = new File(path); OK
if(!file.exists())
file.createNewFile(); //Exception --> FileSystemException: \\\\X.X.X.X\\file-uploads\\80ae9bd7-816e-4544-85d3-fd10679adb7d.jpg: The network path was not found.
Files.write(Paths.get(path), attachmentRequest.getFile().getBytes());
File file = new File ("D:\\Folder\\Folder2\\");
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
The following code supposed to open Folder2, but instead it opens D:\Folder\Folder2.bat file.
How to fix that?
Opening folder through Deskopt.open() would be delegated to Desktop.browseFileDirectory() (JDK 8/9)
But, as seen in JDK-8233994, that is not supported/implemented for Windows.
So the alternative with explorer.exe is indeed the recommended way:
Process p = new ProcessBuilder("explorer.exe", "/select,D:\\Folder\\Folder2").start();
The Desktop API describes the method open() clearly:
Launches the associated application to open the file. If the specified
file is a directory, the file manager of the current platform is
launched to open it.
Or see the reply of #vonC
I'm using IntelliJ and Spring and Java to locally develop an app on a Mac, and then deploy to a tomcat server on AWS, using Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-1048-aws x86_64).
I'm having trouble specifying the file path so that it works in both environments.
My code is
InputStream fileStream = new FileInputStream("src/main/resources/static/web/data/ReportDates.json");
JsonReader reader = Json.createReader(fileStream);
JsonObject reportDates = reader.readObject();
reader.close();
When I run locally, the file is read in correctly. It is located in:
src/main/resources/static/web/data/ReportDates.json
But when I deploy, that code results in the error message:
java.io.FileNotFoundException: src/main/resources/static/web/data/ReportDates.json (No such file or directory)
The actual location of the file on that machine turns out to be:
/opt/tomcat/webapps/automentor/WEB-INF/classes/static/web/data/ReportDates.json
How can I specify the file path so that it works correctly in both environments?
I have given up on using a single path. #Nicholas Pesa got me thinking -- since I use IDEA, I don't have a fixed WEB-INF folder, so it's easier for me to change the path that should be used than to move the file to a fixed location.
My code now uses:
String filepath = (new File("src/main/resources/static/web/data/ReportDates.json").exists()) ? "src/main/resources/static/web/data/ReportDates.json" : "/opt/tomcat/webapps/automentor/WEB-INF/classes/static/web/data/ReportDates.json";
It seems that my code won't work after learning that the machine that I'll be pointing the upload path to is a Linux box.
My use case is, a user logs in to the web app, chooses a file to upload, then click upload button. Is it possible to do this direct from the Java code to the Linux server using appropriate ssh or scp libraries if there is any?
EDIT: Here's my current code.
#Override
public void fileTransfer(File uploadedFile, String fileName, String pathTemp) {
File destFile = new File( pathTemp + File.separator + fileName);
try{
FileUtils.copyFile(uploadedFile, destFile);
String getTempFile = destFile.toString();
String tempPath = getTempFile.replace("\\", "\\\\");
File tempFile = new File(tempPath); // 1st file
String tempFileName = tempFile.getName();
String fileSave = getUploadPathSave().replace("\\", "\\\\");
tempFile.renameTo(new File(fileSave + tempFileName));
} catch (IOException ex) {
System.out.println("Could not copy file " + fileName);
ex.printStackTrace();
}
}
If your app is deployed at one place only (not mass distribution), the easiest way would be:
create samba share on linux machine
map samba share to logical drive on windows machine
do usual file copy with java functions.
attention: renameTo will not work between drives. You'll need to copy input stream to output stream or, better, use apache commons-io functions for that.
There are different possibilities:
If you can create a shared directory in linux and mount it under windows (see Samba. Then you can write to that directory like a local directory. File will go to the linux server.
Use a library like Jsch to upload the file from windows server to linux server.
There are certain things you can do:
1-> If you can program your linux server, then you can make a program that listens to user request on a port, and stores data in file. Then you can send you files to that port of server.
2-> The other way is you can use some sort of script to create ssh-connection to server and then you can just add file through ssh, but here your java program will not be useful.
I personally use my own program to share files between 2 machines in same network.
You can use it,if it will be useful for you: https://github.com/RishabhRD/xshare
I am using java webservice (on tomcat).
I have the following code that handles image upload:
public String uploadPicture( long xId,
int pictureIndex,
String imageData )
{
File imageFile = new File( new String( "D:\\" + xId + "_" + pictureIndex ) );
try
{
FileOutputStream fos = new FileOutputStream( imageFile );
byte[] encodedImage = Base64.decode( imageData );
fos.write( encodedImage );
fos.close();
return imageFile.getPath();
}
catch( FileNotFoundException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch( Base64DecodingException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
I specify the path as D:\ since it is on the local PC.
But I need to update it the the path on the server where it will be deployed - then should change it to ~\picDir? something like that?
The webservice url: http://192.168.0.11:8080/XWebService/services/XWebService
will be updated to domain instead of the 192.168.0.11
What should be the URL to get the image? (E.g. if the picture folder is: ~\picDir)
If target server will run Linux/Unix, then proper path should be something like /usr/share/myapp. '~\' is totally wrong, I guess you meant '~/' which will point to home folder of current user. This should be avoided since you might run web server as different users with different home directories. Usually, on each environment (developer machine, demo, live server) you should have such place for storing configuration and data needed by an application.
File System location of your pictures has nothing to do with the URL under which photos will be located. It depends on Web Server (Tomcat, Jetty, JBoss, etc.) which will run your application and your application itself. For instance, you can configure your Tomcat server to map domain www.myapp.com to /var/lib/tomcat6/webapps/myapp/ directory. Servlet which will publish images might take them from configuration dir mentioned in 1. = /usr/share/myapp/picDir. If the servlet can be accessed via /pictures?picId=1 then you will find them under www.myapp.com/pictures?picId=1. However, if you just want to put static images inside your *.war file to be accessed by the browser, put them in root directory of your *.war file.
To summarize:
Choose (and tell us) your application server
Use some configuration directory for all environments and configure your server to be able to see it
Configure your server for desired domain
You should read more about context of *.war files and how the file itself is being organised.
Understanding URLs and context on example of Tomcat
Assuming that:
On your local machine desired servlet is located under: http://localhost:8080/myapp/utils/myservlet.html
Your app is packed as myapp.war
Remote Tomcat has IP 2.2.2.2 and is running on port 8080
When you deploy your myapp.war to remote Tomcat into webapps directory (/var/lib/tomcat6/webapps) it will get unpacked and you will be able to see your servlet under http://2.2.2.2:8080/myapp/utils/myservlet.html. By configuring your application in Tomcat's server.xml you can add domain name and reduce unnecessary "myapp" part called context, effectively leaving URL in form of http://www.myapp.com/utils/myservlet.html. This is what you want in production environment. This topic is explained in Tomcat's documentation, please refer to it.
Accessing File System resources from web application
If you would like to save or get any file from your server, please keep in mind that client (Web Browser) has no idea about underlying disk structure. The browser uses request-response communication pattern which (in terms of upload/download) can be handled by server like this:
upload - grab some byte content from Request and save it as a file on server file system
download - read some byte content from server file system and stream it as a Response
As you can see in both cases server file system is internal concern of the server itself. You can save it anywhere you want. You can read bytes from whatever location. That is why it's good to have MYAPP_CONF (mentioned in comments) to store and read those files always from some predefined directory.