I am trying to open a file on a Grails server but the problem is that my path contains special characters such as Ç
It worked fine on my OSX environment, but since I uploaded it to the linux production environment, the error "No such file or directory" pops out
The locale is set to fr_FR.UTF-8
EDIT:
Here is the code I am using to reach the file
def reportSystemPath = (pathPrefix + "/${report.cluster.name}" + report.path).replaceAll("//", "/")
Path path = Paths.get(reportSystemPath)
def bytes = byte[]
path.toFile().withInputStream() {
bytes = it.getBytes()
}
Related
Currently I am using Apache POI to write output in Excel in my Java code
here, I am saving this output Excel file somewhere in my local drive.
Now challenge is, I have to save/create this same Excel on Unix server in some directory due to some requirement.
Can anyone suggest how to write code for this?
How to set this Unix server directory path in Java code?
String excelFileName = "D://TestResult_Output_Excel.xlsx";//name of excel file
Save both the windows and unix destinations being sure to save these with a path separator on the end (/ for unix or \\ for windows)
String fileName = "TestResult_Output_Excel.xlsx"
String windowsFilePath = "D:\\outputfolder\\"
String unixFilePath = "/home/foo/folder/"
Get the operating system name where the program is running
String OS = System.getProperty("os.name");
Compare that to your OS options (Windows/UNIX in this case)
String outputFilePath;
if (OS.toUpperCase().contains("WINDOWS")) {
outputFilePath = windowsFilePath;
} else {
outputFilePath = unixFilePath;
}
If you need the file name to be dynamic you can add it at this point, or you could have added it to the original path variables.
outputFilePath += fileName;
I'm pentesting a Java web application that uploads a file to the server. It uses an old library - com.jspsmart.upload.smartupload - which as far as I know should work. The saveAs method requires a path where the file should be saved. I've tried uploading files and cannot get it to work. I feel like the issue might be with the file path. If the install directory is something like C:\my_app the path would be C:\my_app\uploads/filename.txt. Does this matter? Or might something else be causing it not to upload? The upload code is surrounded by a try/catch and doesn't return an exception, so I'm not sure why it wouldn't work.
Some documentation I found on the library: http://demo.easyhis.com/jspsmartupload/help/doc/com/jspsmart/upload/File.html
Sample code:
private static string FS = System.getProperty("file.separator");
try {
myFile = myUpload.getFiles().getFile(0);
importFolder = Globals.getInstallDir() + FS + "uploads";
myFile.saveAs( importFolder + "/" + request.getParameter("import_file_name"), SmartUpload.SAVE_PHYSICAL);
} catch (Exception e) {
// outputs error
}
Java can indifferently use / or \ as a file separator under Windows, but it is one or the other -- don't do both!
Anyway, use Path, you will then be sure that you get a correct path to work with:
Paths.get(Globals.getInstallDir()).resolve(FS).resolve("uploads")
.resolve("import_file_name").toString();
Note that FS should NOT be absolute here (ie, no beginning / or \).
Use File.separator. It returns the system's default path to a specific file separator.
private static string FS = File.separator;
I have uploaded my maven/spring project on jelastic and using following to save images:
ServletContext servletContext = request.getSession().getServletContext();
String absoluteFilesystemPath = servletContext.getRealPath("/");
byte[] fileData = file.getBytes();
String name=Trader.getImage();
if (fileData.length != 0) {
String fileName =login.getUserName()+".jpeg";
File f = new File(absoluteFilesystemPath+"\\img\\"+fileName);
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
fileOutputStream.write(fileData);
fileOutputStream.close();
}
It is working on localhost images are saved in img folder while on server it is saved on absolute path as name "img/xyz.jpeg"
i want to save it on
myproject/img/
It is saving on
myproject
Isn't your \ the wrong way? I'm going to guess that your localhost is Windows?
Jelastic is Linux based, so filesystem paths contain / not \. Maybe try
File(absoluteFilesystemPath+"/img/"+fileName);
Edit: according to File.separator vs Slash in Paths you might want to use File.separator instead for proper platform independence.
I tried the use of Path interface;
//get a path object with relative path
Path filePath = Paths.get("C:\\Test\\filename.txt");
System.out.println("The file name is: " + filePath.getFileName());
Path filePath2 = Paths.get("/home/shibu/Desktop/filename.txt");
System.out.println("The file name is: " + filePath2.getFileName());
The out put is like;
The file name is: C:\Test\filename.txt
The file name is: filename.txt
For the windows file it printed full path and for linux file it printed only file name.
Why this difference?
Simple: On Linux, the only illegal characters in a file name are / and the 0 byte. Everything else, including \, line feed and escape sequences, are valid.
That means C:\Test\filename.txt is a valid file name on Linux. The Java runtime doesn't attempt to be smart and guess that this might be a Windows path.
Note that this is different when you use /: This is a valid path delimiter on Windows when using Java. So the path a/foo.txt is a relative path both on Linux and Windows.
This means you can open files on Windows using Paths.get("/C:/Test/filename.txt");, for example.
I developp a Java application using Windows Desktop Search from which I can retrieve some information about files on my computer such as urls (System.ItemUrl). An example of such url is
file://c:/users/ausername/documents/aninterestingfile.txt
for "normal" files. This field give also urls of mail items indexed from Outlook or Thunderbird. Thunderbird's items (only available using vista and seven) are also files (.wdseml). But outlook's items urls start with "mapi://" like :
mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto#mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가
The problem I have is opening the real item from Java in Outlook using this url. If I copy/paste it in the run dialog of Windows, it works ; it also works if I use "start" followed by the copied/pasted url in command line.
The url seems to be encoded in UTF-16. I want to be able to write such code :
String url = "mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto#mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가";
Runtime.getRuntime().exec("cmd.exe /C start " + url);
I doesn't work and I've tried other solutions like :
String start = "start";
String url = "mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto#mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가";
FileOutputStream fos = new FileOutputStream(new File("test.bat");
fos.write(start.getBytes("UTF16");
fos.write(url.getBytes("UTF16"));
fos.close();
Runtime.getRuntime().exec("cmd.exe /C test.bat");
without any success. Using the solution above, the file "test.bat" contains the correct url and the "start" command, but the run of "test.bat" results in the well known error message :
'■' is not recognized as an internal or external command, operable program or batch file.
Has anybody an idea to be able to open "mapi://" items from Java ?
Well, my question was a little bit tricky. But I finally found an answer and will share it here.
What I suspected was true : Windows uses UTF-16 (little endian) urls. It makes no differences working in UTF-8 when we only use paths to files such as images, text, etc. But to be able to access Outlook items, we must use UTF-16LE. If I were coding in C#, there wouldn't have been any problem. But in Java, you have to be more inventive.
From Windows Desktop Search, I retrieve this:
mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto#mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가
And what I did is creating a temporary VB script and run it like this:
/**
* Opens a set of items using the given set of paths.
*/
public static void openItems(List<String> urls) {
try {
// Create VB script
String script =
"Sub Run(ByVal sFile)\n" +
"Dim shell\n" +
"Set shell = CreateObject(\"WScript.Shell\")\n" +
"shell.Run Chr(34) & sFile & Chr(34), 1, False\n" +
"Set shell = Nothing\n" +
"End Sub\n";
File file = new File("openitems.vbs");
// Format all urls before writing and add a line for each given url
String urlsString = "";
for (String url : urls) {
if (url.startsWith("file:")) {
url = url.substring(5);
}
urlsString += "Run \"" + url + "\"\n";
}
// Write UTF-16LE bytes in openitems.vbs
FileOutputStream fos = new FileOutputStream(file);
fos.write(script.getBytes("UTF-16LE"));
fos.write(urlsString.getBytes("UTF-16LE"));
fos.close();
// Run vbs file
Runtime.getRuntime().exec("cmd.exe /C openitems.vbs");
} catch(Exception e){}
}