user3495652
user3495652

Reputation: 21

File Upload handler

This is a code of File Upload Handler This is working Fine on my server but after uploading file it only displays File Uploaded successfully
but i also want to display the name of file which is uploaded

 import java.io.File;
 import java.io.IOException;
 import java.util.List;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.apache.commons.fileupload.FileItem;
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadHandler extends HttpServlet {
private final String UPLOAD_DIRECTORY = "C:/uploads";

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    //process only if its multipart content
    if(ServletFileUpload.isMultipartContent(request)){
        try {
            List<FileItem> multiparts = new ServletFileUpload(
                                     new DiskFileItemFactory()).parseRequest(request);

            for(FileItem item : multiparts){
                if(!item.isFormField()){
                    String name = new File(item.getName()).getName();
                    item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
                }
            }

  //File uploaded successfully

           request.setAttribute("message", "File Uploaded Successfully");
        } catch (Exception ex) {
           request.setAttribute("message", "File Upload Failed due to " + ex);
        }          

    }else{
        request.setAttribute("message",
                             "Sorry this Servlet only handles file upload request");
    }

    request.getRequestDispatcher("/done.jsp").forward(request, response);

}

}

Upvotes: 2

Views: 972

Answers (2)

A_BOSS
A_BOSS

Reputation: 454

this code may help you!

private static String getFileName(Part part) {
    for (String cd : part.getHeader("content-disposition").split(";")) {
        if (cd.trim().startsWith("filename")) {
            String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
            return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
        }
    }
    return null;
}

Upvotes: 0

developerwjk
developerwjk

Reputation: 8659

Store item.getName() in a variable for later use. Since you are using a loop, you'll have to keep track of multiple filenames if there are any.

   String fileNames = ""; //to keep list of filenames
   for(FileItem item : multiparts){
            if(!item.isFormField()){
                fileNames += item.getName() + "; "; //add filename to string
                String name = new File(item.getName()).getName();
                item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
            }
        }
    .....
    .....
    request.setAttribute("message", "File(s) Uploaded Successfully: " + fileNames );

Upvotes: 1

Related Questions