I want to get the file in resource object using UrlResource, but .toUri method encode path in such a way, that there is file exist, but still, it can't get.
My code is:
val filepath = Paths.get(getClass.getClassLoader.getResource("some file4216.pdf").getPath)
val resource : Resource = new UrlResource(filepath.toUri)
val contentType = "application/pdf"
ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename + "\"")
.body(resource)
It will encode space as "%20", but then also it performs encoding and convert "%" into "%2520".
And that's why file not found.
Give below path:
/home/user/Live_Projects/some-project/some-project-api/build/resources/main/some%2520file4216.pdf
I want only %20 not %2520.
Related
I write some tests and to get absolute path from relative path i use this function
private def getAbsolutePath(filePath: String): String = {
getClass.getResource(filePath).getFile
}
and then i do:
println(getAbsolutePath("/parquetIncrementalProcessor/withPartitioning/"))
println(getAbsolutePath("/parquetIncrementalProcessor/withPartitioning/own_loading_id=1/partition_column=test/"))
i get:
/Users/19658296/csp-fp-snaphot/library/target/scala-2.11/test-classes/parquetIncrementalProcessor/withPartitioning/
/Users/19658296/csp-fp-snaphot/library/target/scala-2.11/test-classes/parquetIncrementalProcessor/withPartitioning/own_loading_id%3d1/partition_column%3dtest/
As you can see, instead of =, I get some strange symbol. At the same time, when I try to read these files with a park, he can read the path without %3d, and with %3d he gets the error "Path does not exist".
How can I fix this?
Seems like its URL encoded, maybe because using stuff from files and resources are designed to work with Universal Resource Locators. You can URLDecode it like so:
import java.net.URLDecoder
def getAbsolutePath(filePath: String): String = {
val path = getClass.getResource(filePath).getFile
URLDecoder.decode(path, "UTF-8")
}
I am in the middle of writing a function to obtain the file list inside a particular directory in HDFS. My following code successfully obtain the list
val status = fileSystem.listStatus(new Path("/" + ownerId + folderName ))
status.foreach(x=> println(x.getPath))
In x.getPath, I obtained a list of path
hdfs://localhost:54310/david/12345/account.csv
hdfs://localhost:54310/david/12345/iris.csv
How do I filter the path to obtain the filename account.csv and iris.csv? Note that I am developing in a local environment so there is a possibility that we may get something like below when deploy into remote server.
hdfs://localhost:54310/media/david/12345/iris.csv
which has a deeper path.
This will do, no regex needed
val filepath = x.getPath().toString()
val filename = filepath.substring(filepath.lastIndexOf('/') + 1)
You can use getName
val status = fileSystem.listStatus(new Path("/" + ownerId + folderName ))
status.foreach(x=> println(x.getPath.getName))
I have following code to save an asset in a servler service
ResourceResolver resourceResolver = this.resolverFactory.getAdministrativeResourceResolver(null);
AssetManager assetMgr = (AssetManager)resourceResolver.adaptTo((Class)AssetManager.class);
String newFile = "/content/dam/travel/" + fileName;
assetMgr.createAsset(newFile, is, "image/jpeg", true);
The following line creates the asset with specified mimetype
assetMgr.createAsset(newFile, is, "image/jpeg", true);
I would like to determine the coming file content type based on stream rather than name.
Thank you,
Sri
Iam trying since 1 hour to get the absolute path of a file ony server side:
String path = request.getRequestURL();
JasperCompileManager.compileReportToFile(path+"/test.jrxml",path+"/test.jasper");
this didnt work my expection is :
not found : http\12.0.0.13:8080]\test\test.jrxml wrong syntax in dataname...etc
Try this one in your RemoteServiceServlet class to get the absolute path of any resources placed under war directory that will be actually the path of server directory when deployed on server.
String pngFullPath = this.getThreadLocalRequest().getSession().getServletContext()
.getRealPath("images/1.png");
System.out.println(pngFullPath);
String icoFullPath = this.getThreadLocalRequest().getSession().getServletContext()
.getRealPath("favicon.ico");
System.out.println(icoFullPath);
output:
D:\Workspace\GWTProject\war\images\1.png
D:\Workspace\GWTProject\war\favicon.ico
Now change it as per the placement of test.jrxml file in your project.
here is the project structure:
This is the method I use:
public static String getServerBase(HttpServletRequest req)
{
String scheme = req.getScheme(); // http
String serverName = req.getServerName(); // sub.domain.ac.uk
int serverPort = req.getServerPort(); // 80
String contextPath = req.getContextPath(); // /MyApp
return scheme + "://" + serverName + ":" + serverPort + contextPath;
}
Then simply append your file name.
Is there an Android class that (correctly) encodes URLs containing unicode characters? For example:
Blue Öyster Cult
Is converted to the following using java.net.URI:
uri.toString()
(java.lang.String) Blue%20Öyster%20Cult
The Ö character is not encoded. Using URLEncoder:
URLEncoder.encode("Blue Öyster Cult", "UTF-8").toString()
(java.lang.String) Blue+%C3%96yster+Cult
It encodes too much (i.e. spaces become "+" and path separators "/" become %2F). If I click on a link containing unicode characters with the Dolphin web browser it works correctly, so obviously this can be done. But if I try to open an HttpURLConnection using any of the above strings, I get an HTTP 404 Not Found exception.
I ended up hacking together a solution that seems to work for this, but is probably not the most robust:
url = new URL(userSuppliedPath);
String context = url.getProtocol();
String hostname = url.getHost();
String thePath = url.getPath();
int port = url.getPort();
thePath = thePath.replaceAll("(^/|/$)", ""); // removes beginning/end slash
String encodedPath = URLEncoder.encode(thePath, "UTF-8"); // encodes unicode characters
encodedPath = encodedPath.replace("+", "%20"); // change + to %20 (space)
encodedPath = encodedPath.replace("%2F", "/"); // change %2F back to slash
urlString = context + "://" + hostname + ":" + port + "/" + encodedPath;
URLEncoder is designed to be used to encode form content, not whole URI's. Encoding / as %2F is intentional to prevent user input from being interpreted as a directory, and + is valid encoding for form data. (form data == part of the URI following the ?)
Ideally, you would encode "Blue Öyster Cult" before appending it to your base URI, instead of encoding the whole string. And if "Blue Öyster Cult" is part of the path instead of part of the query string, you have to replace + with %20 yourself. With these restrictions, URLEncoder works fine.