在jsp中使用multipart上传文件

时间:2011-03-16 04:33:56

标签: file-upload

如何在jsp中使用multipart表单上传文件。我已经尝试上传,文件正在上传,但表格中的其他字段如文字/下拉框,其值无法读取

2 个答案:

答案 0 :(得分:0)

<强>更新

这是一个更好的答案以及为什么你正在尝试的东西不起作用:How to upload files to server using JSP/Servlet?


如果您正在使用Tomcat 6之类的容器(或不支持Servlet 3.0规范的任何地方),则必须使用Apache Commons FileUploadcom.oreilly.servlet等库。

答案 1 :(得分:0)

我确信下面的代码工作正常,因为经过充分测试。

JSP文件:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
     <form method="POST" action="UploadServlet" enctype="multipart/form-data" >
                File:
                <input type="file" name="file" id="file" /> <br/>
                Destination:
                <input type="text" name="foldername"/>
                </br>
                <input type="submit" value="Upload" name="upload" id="upload" />
            </form>

    </body>
    </html>

Servlet文件:

    package com.servicehandler;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;

    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 javax.servlet.annotation.MultipartConfig;
    import javax.servlet.http.Part;



    /**
     * Servlet implementation class UploadServlet
     */
    @WebServlet("/UploadServlet")
    @MultipartConfig(fileSizeThreshold=1024*1024*2,//2mb
                     maxFileSize=1024*1024*10,//10mb
                     maxRequestSize=1024*1024*50)//50mb
    public class UploadServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public UploadServlet() {
            super();
            // TODO Auto-generated constructor stub
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.getWriter().append("Served at: ").append(request.getContextPath());
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        {

            response.setContentType("text/html;charset=UTF-8");
            String subfoldername=request.getParameter("foldername");
            String realpath=getServletContext().getRealPath("/");

            uploadFileTolocaldirectory(subfoldername,request);

            uploadFileToserverContextRealpath(subfoldername,realpath,request);

            uploadFileToapplicationdirectory(subfoldername,request);
        }




        public static void uploadFileTolocaldirectory(String foldername,HttpServletRequest request) throws IllegalStateException, IOException, ServletException
        {

          String savePath="C:\\rootdir\\"+File.separator+foldername;
          File fileSaveDir = new File(savePath);
          if (!fileSaveDir.exists()) 
          {
              fileSaveDir.mkdirs();
          }

          Part part =request.getPart("file");
          String fileName = extractFileName(part);
          part.write(savePath + File.separator + fileName);
        }

        public static void uploadFileToserverContextRealpath(String foldername,String realpath,HttpServletRequest request) throws IllegalStateException, IOException, ServletException
        {
          String savepath=realpath+File.separator+foldername;
          File fileSaveDir = new File(savepath);
          if (!fileSaveDir.exists()) 
          {
              fileSaveDir.mkdirs();
          }

          Part part =request.getPart("file");
          String fileName = extractFileName(part);
          part.write(savepath + File.separator + fileName);
        }

        public void uploadFileToapplicationdirectory(String foldername,HttpServletRequest request) throws IllegalStateException, IOException, ServletException
        {

            String path="D:\\yeldiallproject\\Fileuploadusingservlet\\WebContent";
            String savepath=path+File.separator+foldername;
            File fileSaveDir = new File(savepath);
          if (!fileSaveDir.exists()) 
          {
              fileSaveDir.mkdirs();
          }

          Part part =request.getPart("file");
          String fileName = extractFileName(part);
          part.write(savepath + File.separator + fileName);
        }


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

        }