p34 文件上传及注意问题
1. 上传文件
a. 引入2个jar
apache: commons-fileupload.jar组件
commons-fileupload.jar依赖 commons-io.jar
b. 代码
前台jsp
其中,index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
学号: <input name="sno"/><br/>
姓名: <input name="sname"/><br/>
上传照片:<input type="file" name="spicture"/><br/>
<input type="submit" value="注册"/>
</form>
</body>
</html>
UploadServlet.java
package org.student.servlet;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class UploadServlet
*/
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset= UTF-8");
//上传
try {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
//通过parseRequest解析form中的所有请求字段,并保存到items集合中
//即前台传递的sno, sname, spicture,此时就保存在了items中
List<FileItem> items = upload.parseRequest(request);
//遍历items中的数据(sno sname spicture)
Iterator<FileItem> iter = items.iterator();
while(iter.hasNext()) {
FileItem item = iter.next();
String itemName = item.getFieldName();
int sno = -1;
String name = null;
//判断前台字段,是普通form表单字段(sno sname),还是文件字段
//以前都是通过request.getParameter()来取值
//现在时iter.getString()
if(item.isFormField()) {
if(itemName.equals("sno")) { //根据name属性,判断item是哪个
sno = Integer.parseInt(item.getString("UTF-8"));
}else if(itemName.equals("sname")) {
name = item.getString("UTF-8");
}else {
System.out.print("其他字段....");
}
}
else {
//spicture
//文件上传
//文件名 getFieldName是获取普通表单字段的name值
//getName()是获取文件名
String fileName = item.getName();
//获取文件内容并上传
//定义文件路径:指定上传的位置(服务器路径)
//获取服务器路径
//String path = request.getSession().getServletContext().getRealPath("upload");
String path = "C:\\Users\\Administrator\\upload";
File file = new File(path,fileName);
item.write(file);
System.out.println(fileName + "上传成功");
return ;
}
}
}
}
catch(FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}