I have developed a Play framework application on my windows PC and then transferred it onto my Linux box, I'm uploading a video and a photo to the server, this upload process works perfectly on my Windows PC, but doesn't work on the Linux box.
code I'm using in windows:
String root = Play.application().path().toString();
String globalFolderPath = root + "/public/globalUploadFolder/";
File globalFolder = new File(globalFolderPath);
Code I tried in Linux as well as the above code:
String globalFolderPath = "../../public/globalUploadFolder/";
File globalFolder = new File(globalFolderPath);
Is there a something I have to do regarding file paths differently on the Linux box, could it be a permission issue?
I'm lost as to why this is happening.
The problem was solved by using File.separator
Related
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";
I'm creating .csv file and placing it in one location.
String location = "/APPL_TOP2/IMDD/apps/apps_st/appl/xxfin/12.0.0/bin/xe.csv";
Using exact path working fine on Windows but I need it to work on Linux client too.
String location = "$XXFIN_TOP\\12.0.0\\bin\\xe.csv";
If I'm using relevant path on Linux it is not working, showing Error
SQL*Loader-500: Unable to open file(/APPL_TOP2/IMDD/apps/apps_st/appl/xxfin/12.0.0/bin/xe.csv)
SQL*Loader-553: file not found
SQL*Loader-509: System error: No such file or directory
" Client is asking any option to pass relevant path client machine is Linux"
Paths in Linux have slashes which go the other way. So it should be this:
$XXFIN_TOP/12.0.0/bin/xe.csv
I have installed wkhtmltopdf utility and its accessible via mac terminal. But when I'm trying to access it via java code, I get the following error
Cannot run program "wkhtmltopdf": error=2, No such file or directory
I am using this wrapper of wkhtmltopdf https://github.com/jhonnymertz/java-wkhtmltopdf-wrapper
Same code is perfectly running fine in windows system. So i believe issue is something related to tomcat not able to access the wkhtmltopdf utility.
Here is the code that i am using,
Pdf pdf = new Pdf();
pdf.addPage(serverBasePath + "/htmlview", PageType.url);
// Save the PDF
pdf.saveAs(filePath + "\\" + filename);
You need to tell java-wkhtmltopdf-wrapper were the actual program is on the disk. Try this:
WrapperConfig wc = new WrapperConfig("C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.exe");
Pdf pdf = new Pdf(wc);
...
pdf.saveAs(...);
I have integrated Image magick with a Java WebApp and have deployed it on Azure App Service. On azure , I am getting 0kb as output image for a image while the same image gets converted fine on my local machine.
I am using im4java for integration with Image Magick.
Below is the code:
public void compressImage(String imageSource, String destinationPath) throws IOException, InterruptedException,
IM4JavaException {
ConvertCmd cmd = getCommand();
// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.strip();
op.interlace();
op.addRawArgs(compressionRawArguments);
op.gaussianBlur(compressionGaussianBlur);
op.quality(compressedImageQuality);
op.addImage(imageSource); // source file
op.addImage(destinationPath); // destination file
// execute the operation
cmd.run(op);
}
Both imageSource and destination are temp files created using java. I have checked that imageSource file has correct size but after running this code, the destination file is always 0 Kb.
Please advise what could I be doing wrong?
Answering my own question so that It might be helpful for fellow developers who might face this problem.
Azure App Service normally has Windows Server VMs. You can check the OS of your server in web container logs.
Image Magick for windows does not allow conversion of remote http
image urls while for Unix System, it allows so. My Local machine is
MAC So it was working correctly on my local system.
2 Solutions to this problem that I found:
Either you use a linux VM on Azure
In your application, download the image URL to a temp file and
supply the absolute path of that temp file to image magick for
conversion.
I have tried both and are both working.
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