-1

How can i convert File Type into a FileItem Type in Servlet?

File file = new File("/home/kunal/Desktop/docs");        
File[] files = file.listFiles();

I want to convert files to FileItem Type.

Thanks

Kiran
  • 20,167
  • 11
  • 67
  • 99
Kunal
  • 984
  • 4
  • 14
  • 33
  • 4
    The Java core framework does not contain a FileItem type. Please be more specific. – Jon Skeet Jul 26 '11 at 16:03
  • 1
    Are you talking about the FileItem class in commons.fileupload? – fvu Jul 26 '11 at 16:07
  • I am using Servlet. and 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. – Kunal Jul 26 '11 at 16:10
  • yes fuv. I am talking about that only. – Kunal Jul 26 '11 at 16:12
  • FileItem is a class that provides an easy way to grab uploaded files and e.g. copy them to a File, not the other way around. I don't see why "bulk uploading" would not be possible with commons.fileupload so please elaborate. – fvu Jul 26 '11 at 16:17
  • 3
    You can't simply use `File` for file upload. You need a library that does understand multipart file uploads. Since Servlet 3.0 it's integrated into HttpServletRequest. Is that what you mean? – home Jul 26 '11 at 16:18
  • @fuv, How can i use fileUpload for bulk upload? i am new to java, so don't know much about this. – Kunal Jul 26 '11 at 16:24

2 Answers2

4

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:


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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

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.

Steve
  • 4,457
  • 12
  • 48
  • 89
  • I personally find the Servlet 3.0 API more awkward as it does not offer any API-provided facility to figure if the part is actually a file or not and to get the part body as a `String`. Look at the differences between the both implementations in this answer: http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824 – BalusC Jul 26 '11 at 16:56