对于form enctype= multipart/form-data 二进制流上传文件,服务器端获取不到表单中其他数据的解决办法。

W3school对enctype的解释如下:

描述

application/x-www-form-urlencoded

在发送前编码所有字符(默认)

multipart/form-data

不对字符编码。

在使用包含文件上传控件的表单时,必须使用该值。

text/plain

空格转换为 "+" 加号,但不对特殊字符编码。

 

设置enctype为multipart/form-data值后,不对字符编码,则数据通过二进制的形式传送到服务器端,这时如果用request是无法直接获取到相应表单的值的,而应该通过stream流对象,将传到服务器端的二进制数据解码,从而读取数据。

如果要上传文件的话,是一定要将encotype设置为multipart/form-data的。

解决办法如下:

<span style="color:#333333;">package com.work.services;
import javax.servlet.*;  
import javax.servlet.http.*;  

import java.io.*;  
import java.util.*;  
import org.apache.commons.fileupload.*;  
import org.apache.commons.fileupload.servlet.*;  
import org.apache.commons.fileupload.disk.*;  

import com.work.tool.ReadExcel;
import com.work.tool.Salary_Relation;
import com.work.tool.SelectData;
 
// Servlet 文件上传  
public class UploadServlet extends HttpServlet  
{  
    private String filePath;    // 文件存放目录  
    private String tempPath;    // 临时文件目录  
 
    // 初始化  
    public void init(ServletConfig config) throws ServletException  
    {  
        super.init(config);  
        // 从配置文件中获得初始化参数  
        filePath = config.getInitParameter("filepath");  
        tempPath = config.getInitParameter("temppath");  
 
        ServletContext context = getServletContext();  
 
        filePath = context.getRealPath(filePath);  
        tempPath = context.getRealPath(tempPath);  
        System.out.println("文件存放目录、临时文件目录准备完毕 ...");  
    }  
      
    // doPost  
    public void doPost(HttpServletRequest req, HttpServletResponse res)  
        throws IOException, ServletException  
    {  
        res.setContentType("text/plain;charset=gbk");  
        PrintWriter pw = res.getWriter();  
        try{  
            DiskFileItemFactory diskFactory = new DiskFileItemFactory();  
            // threshold 极限、临界值,即硬盘缓存 1M  
            diskFactory.setSizeThreshold(10 * 1024);  
            // repository 贮藏室,即临时文件目录  
            diskFactory.setRepository(new File(tempPath));  
          
            ServletFileUpload upload = new ServletFileUpload(diskFactory);  
            // 设置允许上传的最大文件大小 4M  
            upload.setSizeMax(10 * 1024 * 1024);  
            // 解析HTTP请求消息头  
        	List fileItems = upload.parseRequest(req);  
//        	DiskFileUpload fu=new DiskFileUpload();
//        	fu.setFileSizeMax(4*1024*1024);
//        	fu.setSizeThreshold(4096);
//        	fu.setRepositoryPath(tempPath);
//        	
//            List fileItems = fu.parseRequest(req);  
            Iterator iter = fileItems.iterator();  
            while(iter.hasNext())  
            {   
                </span><span style="color:#ff0000;">String types;</span><span style="color:#333333;">
                FileItem item = (FileItem)iter.next(); 
               </span><span style="color:#ff0000;"> if("types".equals(item.getFieldName())){
                	types=item.getString();
                }</span><span style="color:#333333;">
                if(item.isFormField())  
                {  
                    System.out.println("处理表单内容 ...");  
                    processFormField(item, pw);  
                }else{  
                    System.out.println("处理上传的文件 ..."); 
                    String filename = item.getName(); 
                    //processUploadFile(item, pw);    
                    if(filename.contains(":\\")){
                    	filename=filename.substring(3);
                    }
                    
                    File file=new File(filePath,filename);
                    item.write(file);                      
                    String ExcellPath=file.getPath();
                    ReadExcel readexcel =new ReadExcel();
                    List list=readexcel.readExcell(ExcellPath);
                    SelectData selectdata = new SelectData();
                    int m=selectdata.Sal(list);
                    System.out.println(m);
                    Salary_Relation salary_relation = new Salary_Relation();
                    int n=salary_relation.Sav(list);
                    System.out.println(n);                    
                }  
            }// end while() 
            RequestDispatcher rd=req.getRequestDispatcher("success.jsp");
            rd.forward(req, res);
            pw.close();
        }catch(Exception e){  
            System.out.println("使用 fileupload 包时发生异常 ...");  
            e.printStackTrace();  
        }// end try ... catch ...  
    }// end doPost()  

	// 处理表单内容  
    private void processFormField(FileItem item, PrintWriter pw)  
        throws Exception  
    {  
        String name = item.getFieldName();  
        String value = item.getString();          
        pw.println(name + " : " + value + "\r\n");  
    }  
      
    // 处理上传的文件  
    private void processUploadFile(FileItem item, PrintWriter pw)  
        throws Exception  
    {  
        // 此时的文件名包含了完整的路径,得注意加工一下  
        String filename = item.getName();         
        System.out.println("完整的文件名:" + filename);  
        int index = filename.lastIndexOf("\\");  
        filename = filename.substring(index + 1, filename.length());  
 
        long fileSize = item.getSize();  
 
        if("".equals(filename) && fileSize == 0)  
        {             
            System.out.println("文件名为空 ...");  
            return;  
        }  
    }  
      
    // doGet  
    public void doGet(HttpServletRequest req, HttpServletResponse res)  
        throws IOException, ServletException  
    {  
        doPost(req, res); 
    }  
} </span>
form表单传过来types类型,用红色标记代码处解决。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值