I have org.springframework.web.multipart.commons.CommonsMultipartFile object and i need to get absolute file path on client device. Thanks
I need to show picture to user, without saving on server.
You are not allowed to get the full path of client device, maybe you can spy the user's username or OS what he dont like you to know.
Use FileApi from Javascript instead (How can I draw an image from the HTML5 File API on Canvas?).
Most browsers will not include the path information on the client.
For example if you have a CommonsMultipartFile you can call getFileItem() to receive a FileItem and then from the FileItem call getName().
For example:
commonsMultipartFile.getFileItem().getName();
However as the documentation states:
Returns the original filename in the client's filesystem, as provided
by the browser (or other client software). In most cases, this will be
the base file name, without path information. However, some clients,
such as the Opera browser, do include path information.
Related
I develop a web application using JDeveloper. Then, my scenario is I want to get a file from client directory (e.g. C://Image.jpg). What I want to achieve is the client's directory defined programmatically. So, I used InputStream, but it will search a file in server directory. if I used UploadedFile, I don't know how to define it. Note that I don't want to use InputFile.
Does anyone have a solution for me?
Search for HTTP File Upload. You need an <input type='file'> control on your webpage, and form encoding set as enctype='multipart/form-data'.
Generally, you can't control the default directory where the browser is going to open a file chooser -- it normally starts from the "user home" directory, but other dirs can be navigated to.
I need a download a text/plain file in to a folder. The url does not end with .txt but it has content-type etc... properly set. When I use the browser it immediately prompts me to save the file. The browser automatically puts proper file name also.
Using java how can i download that url in to a folder? Note that I dont know the filename also but I want the file to be saved in a directory.
code to download a file is easy... my question is that I dont know by what name should i save my file. the filename is part of content-disposition header, now how do i extract that?
The HTTP protocol uses the HTTP headers to define some information about the data transferred.
You have the content-disposition header that can have a property filename that is generated by the server. This holds the name of the file being transferred. But it is optional. Should you handle the case it is not present. Here is the doc: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html
Depending on how you download the file, you'll have dozen of ways to retrieve this file name from the http header.
Give a look to the apache http client for instance.
HIH
M.
I need to know the Java equivalent of PHP's $_SERVER['DOCUMENT_ROOT'].
I am writing a Java backend program that will take an uploaded image as a byte[] and save the image to the server where my Java program is running. Then I want to send the image url to the user (front-end) or as email; so that the user can click on the url I send to view the image.
In PHP, I use $_SERVER['DOCUMENT_ROOT'] plus the relative path of the file. How do I do that in Java?
I am not using servet. I am using Jersey to return the url. (If that's important)
I need the returned path to be "http://localhost:8080/mypics/pic1.jpg" when the server is localhost and "http://www.mysite.com/mypics/pic1.jpg" when running on a live server. The new File("./") code is not returning "http..."; netbeans cannot find the method ServletContext.getContextPath(), after importing
import javax.servlet.ServletContext;
You can use ServletContext.getContextPath() to get context path or use ServletContext.getRealPath(String path) to get real path for a given virtual path.
The simplest way if you not use servlet API is to create File object point to the ./:
System.out.println(new File("./").getAbsolutePath());
In this case you should see in console path to your application directory. This could be your $_SERVER['DOCUMENT_ROOT'] for this application.
I am trying to get file attributes present in a Unix server and when I type this url in my IE it displays the files in the file-folder-directory architecture.
I am planning to write a code for a tool such that I can automate the process of getting the file attributes like file modified date,size of file etc.
Are there any Methods/ways to do this?
Does this code work:
File file = New File("http://<someserver.com>:<portnumber>/logs/log.txt");
Date date = file.LastModifiedDate();
System.out.println("modifed date is"+date);
If the protocol your server supports is only HTTP, I'm afraid there's no easy way to do this. You will have to:
parse the returned HTML, probably looking for <a href= tags (using some html parser, but not with regex)
open those links with new URL(url).openConnection(), read their streams, and do the same thing recursively, until an actual file (and not directory) is found.
But this won't give you the file attributes - only the name and the file contents.
If you want to browse, you need a different protocol, like FTP or SCP.
The HTTP protocol won't help you here. HTTP does not publish any file attributes, except for the Content-length (file size) and the Last-Modified header value which doesn't necessarily mirror the actual file modification date. Also it might not be sent by the HTTP Server at all.
Your best bet would be using an FTP library for example the one from Apache Commons Net.
If you decide to use this library, you can use the properties of the FTPFile class, for example the file size, file date and permissions.
Is there any way of just getting the content of the browsed file without any upload/file transfer operations? I currently use ICEFaces inputFile component but I do not need the default uploading operation of the file.
Thanks.
That's not possible. The client needs to send (upload) the file content along the request body to the server side whenever you want to have the file content at the server side.
If you'd expect that you can solve this by passing only the file path around and use the usual java.io.File stuff and so on, then you're on the wrong track. Imagine that I am the client and I have a c:/passwords.txt, how would you as being the server at the other end of the network ever get its content by java.io.File?
I don't thnik this is possible. Browsers do not allow any file transfer from the client to the server without user interaction.
Tough, if you do not stick to IceFaces, it may be possible to achieve this by writing an applet, wich is granted the necessary permissions.