Is it possible upload file through jsp.
var filepathhere = "http:// xxxx.com/--------/upliaded.pdf".
for exm: http:// xxxxx/test.jsp?file = filepathhere.
can i uses like this, is it possible to by using the path we can upload this.
Accept URL from user in HTML form.
POST URL to servlet , try to read File from passed URL check its type and other validations.
Read file from that URL and on write or insert it in to your own DB
File Upload and Download using Java
Yes you can. request.getParameter("file") return Object that you can cast it to String and can download from that URL
something like that :
String file= (String) request.getParameter("file");
URL fileUrl = new URL(file);
and can download from here
You can get the information in the format of string doing it this way but I don't think you can get any other format( like pdf in your example) of data directly from the url. You can off course first download the file to some path and then get it from there.
Related
I am passing an image from a webpage using AJAX request. The image is passed as a Data URI to a Java function as shown below.
public Result upload() {
String dataUri = request().body().asText();
System.out.println(dataUri);
File file = dataUri;
return ok("File uploaded");
}
The problem I am having is I think I need to convert the data URI to a file object in order to pass it into "File file = ". And if I can do that, then the image will be uploaded to the server. Any takers?
Cropped and uploaded! I found another link on stackoverflow that showed me how to convert base64 image to file. I have successfully loaded the cropped file to a directory on the server. Thanks for your help!! The link which has the answer is here:
Convert base64 string to image
I have a little problem with Struts 2 when I try to get the context path :
ServletActionContext.getServletContext().getRealPath("\\WebContent\\resources\\img\\");
I got this path:
C:\Users\killian\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SiteWebAdministrable\WebContent\resources\imgicone.jpg
Why the exact source path ?
Because i need to upload and save images for an admin website to control background and without the actual path i cannot save images in the resources path...
So i save the path with the name and extension in the database (no problem), and i need to save the image in the resource directory (image problem...)
Can someone help me please ? Did i forgot something ?
This question is the answer ?
How do you get the project path in Struts 2?
servletContext.getServletContext().getRealPath("/resources/img/name_of_image.png")
So, passing the "/" to getRealPath() would return you the absolute disk file system path of the /web folder of the expanded WAR file of the project. Something like /path/to/server/work/folder/demo.war/ which you should be able to further use in File or FileInputStream.
Note that most starters don't seem to see/realize that you can actually pass the whole web content path to it and that they often use
String absolutePathToIndexJSP = servletContext.getRealPath("/") + "demo.png";
instead of
String absolutePathToIndexJSP = servletContext.getRealPath("/demo.png");
getRealPath() is unportable; you'd better never use it
Use getRealPath() carefully.
If all you actually need is to get an InputStream of the web resource, better use ServletContext#getResourceAsStream() instead, this will work regardless of the way how the WAR is expanded. So, if you for example want an InputStream of index.jsp, then do not do:
InputStream input = new FileInputStream(servletContext.getRealPath("/demo.png")); // Wrong!
But instead do:
InputStream input = servletContext.getResourceAsStream("/demo.png"); // Right!
Or if you intend to obtain a list of all available web resource paths, use ServletContext#getResourcePaths() instead.
Set<String> resourcePaths = servletContext.getResourcePaths("/");
Almost all java examples showing how to send an email set dummy file path. But actually we don't know the path before file selection. I have already known input=file can't get the full path of the file due to security problems. Then how can I get the path as email function must use path?
Here is the part in most examples that would use file path
String path = "D:\\jar\\java-json.jar";
String fileName = "java-json.jar";
DataSource source = new FileDataSource(path);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
Have you tried Apache Commons Email library?
It has features for email attachments, and it has built-in support for java servlet integration.
OK, at last it shows most java email examples on Internet are not useful for web browsers as web browser can't get full path of a file. At last I use the InputStream to get the file and put it into the DataSource. These two lines are the keys:
InputStream filecontent = filePart.getInputStream();
DataSource source = new ByteArrayDataSource(filecontent, "Text/txt");
If you are using 'java-mail-1.4.*.jar' then simply do this...
you use 'JFileChooser' to create file chooser popups, then may be (say) in your actionListner method for a button do the following -
filepath = fc.getSelectedFile().getAbsolutePath();
where 'filepath' is a String, and 'fc' is a object to 'JFileChooser' class
My form has an upload button for the user to save file images.
ServletContext context = ServletActionContext.getServletContext();
String appPath = context.getRealPath("");
String filePath = appPath+"\\images\\categories";
File fileToCreate = new File(filePath, getMyFileFileName());
FileUtils.copyFile(getMyFile(), fileToCreate);
I need to upload the picture and put it on the image/categories folder for future access. It is working well in my localbuild but when I deployed it on the cloud by uploading the .war file, it doesn't work anymore. It cannot find the image.
HTML Error upon accessiing the image is as follow
type Status report
message /Teapop/images/categories/team.png
description The requested resource (/Teapop/images/categories/team.png) is not available.
What am I doing wrong?
please try to use File.separator instead
String filePath = appPath+File.separator+"images"+File.separator+"categories";
and please give me some feedback
Hope that helps .
We have an HTTP url location which holds a list of .txt files I want to fetch and hold them as an array of files.
After a dig around in SO, I manage to establish a channel to the URL location hosting the files like this.
String hostedLocation = "http://mydomain.com/file/";
URL url= new URL(hostedLocation);
ReadableByteChannel channel = Channels.newChannel(url.openStream());
I am stuck at this point trying to pull all the files. Can someone help me please.
Thanks
Include commons-io as your dependency.
Then :
String content = IOUtils.toString(url.openStream());