servlet3.0多文件上传

    servlet3.0中多文件上传与前面的例子相似,只不过它是遍历Part集合,努力核实Part 中是包含了一个文件,还是一个普通域值。


    客户端编程

     客户端根据需要,设置多个file域,用来接收待上传的文件。


<!DOCTYPE HTML>
<html>
<body>
<h1>Select a file to upload</h1>
<form action="multipleUploads" enctype="multipart/form-data"
        method="post">
    Author : <input name="author"/><br/>
    First file to upload <input type="file" name="filename"/>
    <br/>
    Second file to upload <input type="file" name="filename"/>
    <br/>
    <input type="submit" value="Upload"/>
</form>
</body>
</html>


    服务器端编程

    首先:MultipleUploadsServlet  类的doPost方法要获得HttpServletRequest中的所有Part

    Collection<Part> parts = request.getParts();

   然后:它迭代整个集合和每一个Part,通过调用getContentType方法,查看是否存在content-type标头。

    for (Part part : parts) {

        if (part.getContentType() != null) {

            // save file Part to disk

        }

    } 


package app11a.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet(urlPatterns = { "/multipleUploads" })
@MultipartConfig
public class MultipleUploadsServlet extends HttpServlet {

    private static final long serialVersionUID = 9991L;

    private String getFilename(Part part) {
        String contentDispositionHeader = 
                part.getHeader("content-disposition");
        String[] elements = contentDispositionHeader.split(";");
        for (String element : elements) {
            if (element.trim().startsWith("filename")) {
                return element.substring(element.indexOf('=') + 1)
                        .trim().replace("\"", "");
            }
        }
        return null;
    }

    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {
        
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();

        Collection<Part> parts = request.getParts();
        for (Part part : parts) {
            if (part.getContentType() != null) {
                // save file Part to disk
                String fileName = getFilename(part);
                if (fileName != null && !fileName.isEmpty()) {
                    part.write(getServletContext().getRealPath(
                            "/WEB-INF") + "/" + fileName);
                    writer.print("<br/>Uploaded file name: " +
                            fileName);
                    writer.print("<br/>Size: " + part.getSize());
                }
            } else {
                // print field name/value
                String partName = part.getName();
                String fieldValue = request.getParameter(partName);
                writer.print("<br/>" + partName + ": " + 
                        fieldValue);
            }
        }        
    }
}


你可能感兴趣的:(servlet,upload)