2

I'm using javazoom for uploading

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException {
    PrintWriter out = null;
    JOptionPane.showMessageDialog(null, "Lets do this");
    try {
        response.setContentType("text/html;charset=UTF-8");
        try {
            MultipartFormDataRequest dataRequest = new MultipartFormDataRequest(request);
            //get uploaded files
            Hashtable files = dataRequest.getFiles();
            if (!files.isEmpty()) {
                UploadFile uploadFile = (UploadFile) files.get("filename");
                byte[] bytes = uploadFile.getData();
                String s = new String(bytes);

the files are always coming as empty. Any help please?


I then tried doing this with Apache Commons FileUpload:

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException {
     PrintWriter out = null;

        try {

            response.setContentType("text/html;charset=UTF-8");
            //MultipartFormDataRequest dataRequest = new MultipartFormDataRequest(request);
            //get uploaded files
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            List files = null;
            try {
                files = upload.parseRequest(request);
            } catch (FileUploadException ex) {
                Logger.getLogger(ProcessUploadItem.class.getName()).log(Level.SEVERE, null, ex);
            }

and it failed at files = upload.parseRequest(request);

Any pointers?

Sorry and thank you :)

2
  • Please pay attention to the suggest prompts when tagging your question. Any tag with a number less than 10 after it's name is probably wrong. Avoid creating new tags. Commented Jun 5, 2009 at 14:41
  • ah thnx...I'll keep that in mind Commented Jun 7, 2009 at 13:36

3 Answers 3

4

Check the form sending the file has enctype="multipart/form-data" defined like here:

<form enctype="multipart/form-data" action="...

Other way the file will never upload according to RFC1867

Sign up to request clarification or add additional context in comments.

1 Comment

I wrote this to my other servlet out.println("<form action='ProcessUploadItem' method='post' enctype='multipart/form-data'>"); out.println("<h1>File upload</h1>"); out.println("Select a file:"); out.println("<input type='file' name='filename' />"); out.println("<br />"); out.println("<input type='submit' name='button' value='upload' />"); out.println("</form>");
4

I'd recommend using a more popular, high-profile library to do this, such as Apache Commons FileUpload. It's more likely to work, have better docs and have more people around to help you use it.

Comments

0

Are you using another framework, like Trinidad or similar? They usually include filters that recover the uploaded files so when the process gets to your server, the request doesn't contain any attached file.

1 Comment

No, I don't believe that I am :S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.