sql >> Database >  >> RDS >> Mysql

hoe meerdere afbeeldingen naar een map te uploaden met jsp en servlet

Probeer de onderstaande code (Apache commons-bestand uploaden)

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>

Of U kunt ook onderstaande code gebruiken voor jsp (als u deze code gebruikt druk op ctrl om meerdere afbeeldingen te selecteren)

<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>

En 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();
                }
            }
        }
    }
    }
}

Ik hoop dat dit helpt!!!



  1. Vind het kleinste ongebruikte nummer in SQL Server

  2. Hoe veel rijen samen te voegen met dezelfde id in sql?

  3. PostgreSQL:prestaties van select null vs false

  4. PostgreSQL:SELECTEER WAAR minder dan 15 minuten oud