首先,如果这个form表单中包含文件上传,那么在servlet中你就不能通过request.getparameter(" ")获取form表单内的其他参数了,而是要通过以下方法。
1.首先为了实现文件上传你需要2个jar包
2. 你的form表单要这么写 <form enctype="multipart/form-data" class=“XXX” action="XXXX" method="post" ></form>
3.这里用servlet举了一个例子
@WebServlet("/contractinsert.html")
public class ContractFormUpdateServlet extends HttpServlet{
private static final long serialVersionUID = -820852300256415161L;
private ContractFormService contractFormService=new ContractFormServiceImpl();
private FormatChange formatChange = new FormatChange();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
//2、创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
List<String> pList = new ArrayList<>();
List<FileItem> list = null;
String filename=null;
try {
list = upload.parseRequest(request);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(FileItem item : list){
//如果fileitem中封装的是普通输入项的数据
if(item.isFormField()){
//String name = item.getFieldName();
String value = item.getString("UTF-8");
pList.add(value);//将非文件的其他参数放到一个list中,后面可以顺序的去取到
//System.out.println("name"+name+"value"+value);
continue;
}else{//如果fileitem中封装的是上传文件
InputStream stream=item.getInputStream();//上传文件需要的文件流参数
filename=item.getName(); //上传文件需要的参数
String savepath=getServletContext().getRealPath("/WEB-INF/upload");
File path=new File(savepath); //这个要自己写具体的路径,是需要上传文件需要的参数
Upload.uploadFile(stream, path,filename); //调用工具类方法
if(filename==null || filename.trim().equals("")){
//判空处理}
continue;
}
}
//开始顺序取非文件参数
String project_id=pList.get(0);
String contract_name=pList.get(1);
String customer_company_name=pList.get(2);
String technical_director=pList.get(3);
String date=pList.get(4);
Date contract_date =formatChange.tosqldate(date);
String money=pList.get(5);
BigDecimal contract_money =formatChange.tobigdecimal(money);
String contract_appendices=filename;
int row=contractFormService.contractFormUpdate(contract_name, customer_company_name, technical_director, contract_money, contract_date, contract_appendices, project_id);
if (row>0) {
response.sendRedirect(request.getContextPath()+"/contractlist.html" );
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
4.Upload工具类实现文件上传
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* 说明:由servlet调用,传入(文件流,要上传的路径,文件名)三个参数
* 上传文件的servlet获取表单数据必须使用
* for(FileItem item : list)方法
* @author student
*
*/
public class Upload {
public static void uploadFile(InputStream filestream,File savaPath,String filename){
//使用Apache文件上传组件处理文件上传步骤:
//1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
//解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
//创建一个文件输出流
//注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如: c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
//处理获取到的上传文件的文件名的路径部分,只保留文件名部分
filename = filename.substring(filename.lastIndexOf("\\")+1);
String realSavePath = savaPath+"\\"+filename;
//创建一个输出流
FileOutputStream out = null;
try {
out = new FileOutputStream(realSavePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//创建一个缓冲区
byte buffer[] = new byte[1024];
//判断输入流中的数据是否已经读完的标识
int len = 0;
//循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
try {
while((len=filestream.read(buffer))>0){
//使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
out.write(buffer, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("这才是真正的目录:"+realSavePath);
//关闭输入流
try {
filestream.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭输出流
//删除处理文件上传时生成的临时文件
//item.delete();
}
}