Mysql
 sql >> Base de données >  >> RDS >> Mysql

comment télécharger plusieurs images dans un dossier en utilisant jsp et servlet

Essayez le code ci-dessous (téléchargement de fichiers Apache Commons)

index.jsp

<html>
 <head><title>Upload page</title></head><body>
 <form action="UploadServlet" method="post" enctype="multipart/form-data" name="form1" id="form1">
   <center>
   <table border="1">
       <tr>
           <td align="center"><b>Multipale file Uploade</td>
       </tr>
       <tr>
           <td>
               Select file: <input name="file" type="file" id="file">
           </td>
       </tr>
       <tr>
          <td>
             Select file:<input name="file" type="file" id="file">
          </td>
        <tr>
           <td>
              Select file:<input name="file" type="file" id="file">
           </td>
         </tr>
         <tr>
            <td align="center">
               <input type="submit" name="Submit" value="Submit files"/>
            </td>
         </tr>
    </table>
    <center>
 </form>
 </body>
 </html>

Ou Vous pouvez également utiliser le code ci-dessous pour jsp (si vous utilisez ce code appuyez sur ctrl pour sélectionner plusieurs images)

<html>
 <head><title>Upload page</title></head><body>
 <form action="UploadServlet" method="post" enctype="multipart/form-data" name="form1" id="form1">
   <center>
   <table border="1">
       <tr>
           <td align="center"><b>Multipale file Uploade</td>
       </tr>
       <tr>
           <td>
               Specify file: <input name="file" type="file" id="file" multiple>
           </td>
       </tr>
         <tr>
            <td align="center">
               <input type="submit" name="Submit" value="Submit files"/>
            </td>
         </tr>
    </table>
    <center>
 </form>
 </body>
 </html>

Et UploadServlet.java

package com;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
        } else {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = null;
            try {
                items = upload.parseRequest(request);
                } catch (FileUploadException e) {
                    e.printStackTrace();
                }
            Iterator itr = items.iterator();
            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                if (item.isFormField()) {
                } else {
                    try {
                    String itemName = item.getName();
                    File savedFile = new File("D:\\"+itemName);
                    item.write(savedFile);  
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    }
}

J'espère que cela vous aidera !!!