Re: Uploading multiple files using Oreilly MultipartRequest class

Yuri Singidas <[email protected]>
Newsgroups gmane.comp.java.sun.servlet
Organization OIT
Message-ID <[email protected]>
This is a sample code from a servlet that allows multiple file
attachments. The paths of the different files are placed in a vector:

if (button.equalsIgnoreCase("Attach")) {

               Vector mfiles = (Vector)session.getAttribute("files");

               //upload file to server
                MultipartRequest mr = new
MultipartRequest(request,uploadDirectory);

                Enumeration params = mr.getParameterNames();

                while (params.hasMoreElements()) {
                      String name = (String)params.nextElement();
                      String value = mr.getParameter(name);
                      System.out.println(name + " = " + value);
                }

                boolean attached = false;

                Enumeration files = mr.getFileNames();

                while (files.hasMoreElements()) {
                    String name = (String)files.nextElement();
                    String filename = mr.getFilesystemName(name);
                    if (filename==null){
                        System.out.println("\nDCA_TEC/Attach:File
attached is null");
                        String mailmsg = "Error: No file selected for
attachment.";
                        session.setAttribute("mailmsg",mailmsg);

response.sendRedirect("/NASApp/DCA_TEC/Message.jsp");
                        break;
                    }
                    String type = mr.getContentType(name);
                    File f = mr.getFile(name);
                    System.out.println("\nDCA_TEC/Attach:name:"+name);

System.out.println("\nDCA_TEC/Attach:filename:"+filename);
                    System.out.println("\nDCA_TEC/Attach:type:"+type);
                    if (f!=null){

System.out.println("\nDCA_TEC/Attach:length:"+f.length());
                        path = f.getAbsolutePath();
                        f.deleteOnExit();

System.out.println("\nDCA_TEC/Attach:path:"+f.getAbsolutePath());

                    }
                    //check if attached file is not one of the
following: exe, bat, com, dll
                    //if it is, remove the file from the server
                    boolean allowed = false;
                    String extension =
filename.substring((filename.length()-4),filename.length());
                    String[] allowedfiles =
{".avi",".bmp",".css",".csv",".doc",".dot",".gif",".htm",

"html",".ico",".jpe","jpeg",".jpg",".log",".lzh",".mdb",

".mid",".mp3",".pdf",".pps",".ppt",".rtf",".tar",".tif",

".txt",".wav",".xlb",".xls",".xml",".zip",".ear",".war",

".jar",".snp",".mpp",".mpe","mpeg",".mpg",".wks",".wk1",

".wk3",".wk4",".dbf",".xlt",".xlw",".prn",".spl",".swf",
                                        ".dif",".pcx",".rm" };

                    for(int x=0;x<allowedfiles.length;x++){
                        if (extension.equalsIgnoreCase(allowedfiles[x])){
                            allowed = true;
                        }
                    }
                    if (allowed==false) {
                        if (f.exists()){
                            boolean deleted = false;
                            deleted = f.delete();
                            if (deleted){

System.out.println("\nDCA_TEC/Attach:file deleted");
                            }else {

System.out.println("\nDCA_TEC/Attach:file not deleted.should be removed
manually.");
                            }
                            attached = true;
                            String mailmsg = "Attachment fails. File
selected may be potentially risky.";
                            session.setAttribute("mailmsg",mailmsg);
                        }
                    }
                }

Amit L Kanaskar wrote:

>Hi,
>
>I want to upload some files to the server from a applet. I am using the
>com.oreilly.servlet.MultipartRequest class and it works for a single file.
>Is it possible to do it for multiple files also ?
>It would be very helpful if there is any sample code available. I checked on
>the internet but all the examples are for form based upload.
>
>Thank you in advance,
>amit.
>
>___________________________________________________________________________
>To unsubscribe, send email to [email protected] and include in the body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
>
>
>

___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.