unable to get relative path to absolute path in java - java

Im giving the path of the image to properties file and getting the path to Image class in my pdfGenerating class. like:
properties.load(DownloadPdf.class.getResourceAsStream("/resource.properties"));
System.out.println("--properties----"+properties);
System.out.println("-path-"+properties.getProperty("logoPath"));
//String path = properties.getProperty("logoPath");
//URL uri = Paths.get(path).toUri().toURL();
System.out.println("---path-"+properties.getProperty("logoPath"));
Image image = Image.getInstance(properties.getProperty("logoPath"));
Here my logopath is:
path----------------C:/Users/Home/Documents/workspace-gofindo/.metadata/.plugins/org.eclipse.wst.server.core/.....
But i'm getting the path in
Image.getInstance(properties.getProperty("logopath"));
like
C:\Users\Home\Documents\workspace-gofindo\.metadata\.plugins\org.
I have tried with replace and replaceAll() methods to convert '\' to '/'
again the image class converting into '\'.
How to get my absolute path which i have specified in properties file exactly into Image.getInstance() method

I have remodified the way i can read the path of the file like below.
// Load path of the Image from Properties file
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream(
"/resource.properties"));
// To get the Path of the Context
String contextPath = request.getContextPath();
String split_path[] = contextPath.split("/");
contextPath = request.getRealPath(split_path[0]);
Image image = Image.getInstance(contextPath
+ properties.getProperty("logoPath"));
image.scaleAbsolute(120f, 60f);// image width,height

Related

How to rename a file before insert to a path Java?

I need to insert a file to a path. However, the file name need to change to a specific name before inserted.
How can I change the name of the file before insert to path? As many resources online only able to change the file name after inserted. Online Resource for rename file
My code currently
String localPath = "c://Users/foody/Documents/write_file_local/";
String finalPath = localPath + file.getOriginalFilename();
File uploadPath = new File(finalPath);
if (!uploadPath.getParentFile().exists()) {
uploadPath.getParentFile().mkdirs();
}
//I think need to rename the file here before insert to path
byte[] bytes = file.getBytes();
Path path = Paths.get(finalPath);
Files.write(path, bytes);
Replace your code with this and update your "CustomName_ABC" with your new fileName.
String localPath = "c://Users/foody/Documents/write_file_local/";
String finalPath = localPath + "CustomName_ABC";
File uploadPath = new File(finalPath);
if (!uploadPath.getParentFile().exists()) {
uploadPath.getParentFile().mkdirs();
}
Files.copy(file.toPath(), uploadPath.toPath());
You can copy your old file to a new filePath (directory) by using Files.copy() method. It will take two parameters:
Old file path
New file path

Unable to get the correct file path in android

I am trying to get the file path of a file from a file picker.
My problem is, that when I output the file path in text form, I get the following output:
file:/storage/emulated/~rest_of_path~
I assume, to access the file, the path should begin something like this:
file:///storage/emulated/~rest_of_path~
or at least file://
Why do I only get file:/ i.e a single slash.
The code in charge for getting the file path:
...
File fileToImport = new File(actualfilepath);
String tempPath = uri.getPath();
if (tempPath.contains("//")){
tempPath = tempPath.substring(tempPath.indexOf("//")+1);
}
if ( actualfilepath.equals("") || actualfilepath.equals(" ")) {
fileToImport = new File(tempPath);
}else {
fileToImport = new File("file://"+actualfilepath);
}
// TODO: 2019-06-04
functionforfile(fileToImport)
filePath.setText(fileToImport.toString());
The text view for the code filePath.setText(fileToImport.toString());
shows me the file path for debug purpose only
if fileToImport = new File("file://"+actualfilepath); has File("file://"actualfilepath) instead, it outputs the file path without file:// which I understand, but why do the two // get removed when I add "file://" + actualfilepath
How could I add the correct file:///
Thanks in advance

Converting a File:///... string to an Absolute URI

So I am making an app that lets users pick images from their gallery to display, as well as take photos from the camera.
The image is previewed via a bitmap that takes the string of the file path. When the user takes a photo the code below handles it (and works):
String path = mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg"
File pictureFile = new File(path);
Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
image1.setImageBitmap(bitmap);
The path is of the format like this:
/storage/emulated/0/Pictures/App/IMG_20150512_130719.jpg
However when I try to select from my library, my code is setup so that the string I am returning formatted like this:
file:////storage/emulated/0/Pictures/App/IMG_20150512_130719.jpg
Now obviously I can (and currently am) split the string and get the path to match the above, but I am curious if there is a standard way to convert between these to?
I saw Uri.parse() but that doesn't work because that returns a URI and I want a String that I can make a file from and then get the absolute path.
If you have a clean suggestion on how I can move from the file://// string to the / string I'm all ears.
Thanks
Converting from file uri to file path:
String fileUri = "file:///storage/emulated/0/";
String filePath = Uri.parse(fileUri).getPath();
Converting from file path to file uri:
String filePath2 = "/storage/emulated/0/";
String fileUri2 = Uri.fromFile(new File(filePath2)).toString();

Get path without '~' character

I have a code snippet that get path of felix home like this:
String path = System.getProperty('felix.home')
Felix home place at:
D:\LongFolderName\Implementations\brannches\MoreLongFolerName\
Path will be a string like this:
D:\LONG-F~1\IMPLEM~1\branches\MORE~1\
I want it must be full path without '~' character like:
D:\LongFolderName\Implementations\brannches\MoreLongFolerName\
What i have to do?
Try:
// Groovy
String path = new File( System.properties.'felix.home' ).canonicalPath
or
// Java
String path = new File( System.getProperty('felix.home') ).getCanonicalPath()

url = new java.net.URL()

url = new java.net.URL(s) doesn't work for me.
I have a string C:\apache-tomcat-6.0.29\webapps\XEPServlet\files\m1.fo and need to make a link and give it to my formatter for output, but malformed url recieved. It seems that it doesn't make my string to url.
I want also mention, that file m1.fo file is in files folder, in my webapp\product\, and I gave the full path to string like: getServletContext().getRealPath("files/m1.fo"). What I am doing wrong? How can I recieve the url link?
It is possible to get an URL from a file path with the java.io.File API :
String path = "C:\\apache-tomcat-6.0.29\\webapps\\XEPServlet\\files\\m1.fo";
File f = new File(path);
URL url = f.toURI().toURL();
Try: file:///C:/apache-tomcat-6.0.29/webapps/XEPServlet/files/m1.fo
It isn't preferable to write file:/// . Indeed it works on windows system,but in unix - there were problems.
Instead of using
myReq.put("xml", new String []{"file:" + System.getProperty("file.separator") +
getServletContext().getRealPath(DESTINATION_DIR_PATH) +
System.getProperty("file.separator") + xmlfile});
you can write
myReq.put("xml", new String [] {getUploadedFileURL (xmlfile)} );
, where
public String getUploadedFileURL(String filename) {
java.io.File filePath = new java.io.File(new
java.io.File(getServletContext().getRealPath(DESTINATION_DIR_PATH)),
filename);
return filePath.toURI().toURL().toString();
A file system path is not a URL. A URL is going to need a protocol prefix for one. To reference file system use "file:" in front of your path.

Categories

Resources