java File to FileItem
How can i convert File Type into a FileItem Type in Servlet?
File file = new File("/home/kunal/Desktop/docs")开发者_StackOverflow社区;
File[] files = file.listFiles();
I want to convert files to FileItem Type.
Thanks
I think there's a major misunderstanding here.
As per the comments:
previously i was using FileUpload control in which i was using FileItem type. but now i have to use File Type for bulk upload, so i want to convert File to FileItem.
You are apparently using MSIE browser to test your webapp. MSIE has the misbehaviour that it sends the complete file path instead of only the filename for the <input type="file">
control. And you are apparently in the server side constructing a File
based on the client side base file folder and attempting to call listFiles()
on it to get a list of all files on the client side file system so that you can "bulk upload" them.
This is not going to work. If it was possible, it would have been a huge security hole. You cannot unaskingly retireve a list of all files from the client side file system. It would only work when both the webserver and webbrowser runs at physically the same machine.
See also:
- How to get the file path from HTML input form in Firefox 3
As to your concrete functional requirement, I understand that you want to be able to let the client select a folder and then send all files in that folder to the server side. This is indeed not supported by the HTML4 <input type="file">
control which allows only a single file selection. However, if you target HTML5 browsers (FF >= 3.6, Chrome >= 2, Safari >= 4), then you could use the multiple
attribute
<input type="file" name="upload" multiple="multiple" />
But if you want to support older browsers (and MSIE) as well, your best bet is a Flash or Java Applet solution in order to be able to select a folder or multiple files. For example: Uploadify, SWFUpload, JUpload and JumpLoader.
Regardless, the servlet code should not be changed in any way. It should just stick to utilizing the Apache Commons FileUpload API the usual way as you did before. It'll work perfectly fine for HTML5 multiple
, Flash and Java Applet solutions as well. The only difference is that you now get multiple FileItem
objects, one for each uploaded file.
See also:
- How to select multiple files for upload?
If I understand this right, it sounds like you're trying to upload a file to your servlet using Apache Commons FileUpload. While this doesn't directly answer your question, you might try using the @MultipartConfig annotation, rather than Apache Commons.
@MultipartConfig
public class Foo extends HttpServlet{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Part p = request.getPart("uploadInput");
}
Then you could do p.getInputStream() to read in the file, where "uploadInput" is an upload element on your HTML page. You could then manually build File objects out of that, or do whatever it is you need done. This has the added advantage of not utilizing a third party library, which can sometimes be awkward.
精彩评论