通过将request.getParameter与multipart一起使用来获取值

时间:2014-11-21 09:00:14

标签: java servlets

任何人都可以告诉我为什么我在做getParameter时获得null值...并且请告诉我如何使用multipart.please request.getParameter 来获取数据。

public class UploadServlet extends HttpServlet {
private boolean isMultipart;
private String filePath;
private int maxFileSize = 50000 * 1024;
private int maxMemSize = 50000 * 1024;
private File file;



public void doPost(HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, java.io.IOException {

    isMultipart = ServletFileUpload.isMultipartContent(request);
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    if (!isMultipart) {

        return;
    }
    DiskFileItemFactory factory = new DiskFileItemFactory();
      factory.setSizeThreshold(maxMemSize);
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(maxFileSize);
    try {
       List fileItems = upload.parseRequest(request);
        Iterator i = fileItems.iterator();
        while (i.hasNext()) {
            FileItem fi = (FileItem) i.next();
            if (!fi.isFormField()) {
                String fieldName = fi.getFieldName();
                String fileName = fi.getName();
                String contentType = fi.getContentType();
                boolean isInMemory = fi.isInMemory();
                long sizeInBytes = fi.getSize();
                String filePath="/home/krisnela/Music/";
                if(fileName!="" && fileName!=null){
                          if (fileName.lastIndexOf("/") >= 0) {
                    file = new File(filePath
                            + fileName);
                } else {
                    file = new File(filePath
                            + fileName);
                }
                fi.write(file);
                out.println("Uploaded Filename: " + fileName + "<br>");

                }

            }
        }
           out.println("<h1>"+request.getParameter("himan")+"</h1>");
        out.println("</body>");
        out.println("</html>");
    } catch (Exception ex) {
        out.println(ex);
    }
}

1 个答案:

答案 0 :(得分:-1)

尝试以下代码

公共类UploadServlet扩展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 {

}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       File file = null ;
       int maxFileSize = 5000 * 1024;
       int maxMemSize = 8000 * 1024;
       ServletContext context = request.getServletContext();
       String filePath = context.getInitParameter("file-upload");
       // Verify the content type
       String contentType = request.getContentType();
       if ((contentType.indexOf("multipart/form-data") >= 0)) {

          DiskFileItemFactory factory = new DiskFileItemFactory();
          // maximum size that will be stored in memory
          factory.setSizeThreshold(maxMemSize);
          // Location to save data that is larger than maxMemSize.
          factory.setRepository(new File("c:\\temp"));

          // Create a new file upload handler
          ServletFileUpload upload = new ServletFileUpload(factory);
          // maximum file size to be uploaded.
          upload.setSizeMax( maxFileSize );
          try{ 
             // Parse the request to get file items.
             List fileItems = upload.parseRequest(request);

             // Process the uploaded file items
             Iterator i = fileItems.iterator();


             while ( i.hasNext () ) 
             {
                FileItem fi = (FileItem)i.next();
                if ( !fi.isFormField () )   
                {
                // Get the uploaded file parameters
                String fieldName = fi.getFieldName();
                String fileName = fi.getName();
                boolean isInMemory = fi.isInMemory();
                long sizeInBytes = fi.getSize();
                // Write the file
                if( fileName.lastIndexOf("\\") >= 0 ){
                file = new File( filePath + 
                fileName.substring( fileName.lastIndexOf("\\"))) ;
                }else{
                file = new File( filePath + 
                fileName.substring(fileName.lastIndexOf("\\")+1)) ;
                }
                fi.write( file ) ;
                }
             }
             // code for xlsx files
            InputStream ExcelFileToRead = new FileInputStream(file);
            XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);
            XSSFWorkbook test = new XSSFWorkbook(); 
            XSSFSheet sheet = wb.getSheetAt(0);
            XSSFRow row; 
            XSSFCell cell;
            Iterator rows = sheet.rowIterator();
                String columns = "";
                row=(XSSFRow) rows.next();
                Iterator cells = row.cellIterator();
                while (cells.hasNext())
                {
                    cell=(XSSFCell) cells.next();
                    columns += cell+",";
                }


                request.setAttribute("columns", columns);
                HttpSession session=request.getSession(true); 
                session.setAttribute("file",file);

                RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("/excel.jsp");
                reqDispatcher.forward(request,response);
          }catch(Exception ex) {
             ex.printStackTrace();
          }
       }
}

}

web.xml中的

<context-param>
    <description>Location to store uploaded file</description>
    <param-name>file-upload</param-name>
    <param-value>
        D:\
     </param-value>
  </context-param>