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类型,用红色标记代码处解决。