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());
Related
I have thousands of images in my folder on my computer and I am trying to find out how can I check if the file from given URL is already downloaded. Is is possible somehow?
This only give me size of the file.
URL url = new URL("http://test.com/test.jpg");
url.openConnection().getContentLength();
For duplicate file I use
FileUtils.contentEquals(file1, file2)
Thank you for your answers!
If you have a base URL and store files with the same filenames. You can ask the server if it's worth downloading the image again thanks to the file modification time and the If-Modified-Since HTTP Header.
File f = new File();// the file to download
HttpURLConnection con = (HttpURLConnection) new URL("http://www.test.com/"+f.getName()).openConnection();
// Add the IfModifiedSince HEADER
con.setIfModifiedSince(f.lastModified());
con.setRequestMethod("GET");
con.connect();
if(con.getResponseCode() == 304) {
System.out.println(f+ " : already downloaded");
} else {
// Download the content again and store the image again
}
It will work if the modification time of the local file has been left intact since the first download and if the server supports IfModifiedSince header.
If you don't know how to match the filename and the URL then there is no obvious way to it.
You could do some experiments with a fast HEAD request and extract some relevant informations like :
Content-Length
Last-Modified
ETag
Content-Length + Last-Modified could be a good match.
For ETags if you know how the http server builds the ETag you could try to build it on your side (on all your local files) and use it as a value to compare.
Some info on ETags:
http://bitworking.org/news/150/REST-Tip-Deep-etags-give-you-more-benefits
https://serverfault.com/questions/120538/etag-configuration-with-multiple-apache-servers-or-cdn-how-does-google-do-etag
Unfortunately ETag can be constructed with informations only visible to server (inode number) so it will be impossible for you to rebuild it.
It will certainly be easier/faster to download your files again.
If you don't download the file you can't compare it with another.
Otherwise you can store the content you downloaded in a temp file:
File temp = new File(FileUtils.getTempDirectory(), "temp");
FileUtils.copyURLToFile(url, temp);
then loop through your existing files and call:
FileUtils.contentEquals(temp, existingFile)
In the end you would want either to keep or delete the temp file.
Of course this is not very fast. If you have thousands of files, you could save their hashes in a file and use that instead of FileUtils.contentEquals.
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 .
I have a homepage, which has a directory for downloads.
I want to read all files of this homepage automatically, from its directory.
For example I have the homepage: www.homepage.org and the subdirectory resources/downloads with the download files I want to show.
I have a Java Server with Spring and JSPs running and tried the following, which didn't work at all:
String path = request.getContextPath();
path += DOWNLOADS;
URL url = null;
String server = request.getServerName();
try {
url = new URL("https", server, request.getLocalPort(), path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (url != null) {
String externalForm = url.toExternalForm();
File directory = new File(externalForm);
String[] files = directory.list();
for (String file : files) {
;
}
}
You can use a Servlet. If your resources directory is in your web app's root directory, then use
PrintWriter writer = response.getWriter();
String path = getServletContext().getRealPath("/resources/downloads");
File directory = new File(path);
if(directory.isDirectory()){
String[] files = directory.list();
for (String file : files) {
writer.write(file + "<br/>");
}
} else writer.write(directory.getAbsolutePath() + "could not be found");
I found a solution with using a FTP manager...
You can view the code here
I hope it will help you resolve the problem.
You need to list the directory using the path to the directory not the url. Once you retrieve the files you must create your own html to put it your web if that's what you want.
There is a good example to create a directory listing and browsing with JQuery here
There is a JSP connector included in the package in the download section that you may need to tune up a bit if you don't want to use the JQuery tree.
good luck!
The HTTP protocol does not provide a way to list the "files" in a "directory". Indeed, from the perspective of the protocol spec, there is no such thing as a directory.
If the webserver you are trying to access supports WebDAV, you could use PROPFIND to find out a collection (i.e. directory)'s structure.
Alternatively, if the webserver has directory listing enabled (and there is no index.html or whatever) then you could screen-scrape the listing HTML.
Otherwise, there is no real solution ... using HTTP / HTTPS.
On rereading the question and the answers, it strikes me that the servlet that is trying to retrieve the directory listing and the servlet that hosts the directories could actually be running on the same machine. In that case, you may have the option of accessing the information via the file system. But this will entail the "client" servlet knowing how the "server" servlet represents things. The URLs don't (and cannot) provide you that information.
The answer provided by rickz is one possible approach, though it only works if the client and server servlets are in fact the same servlet.
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.