servlet文件上传

1.获取文件上传路径

String path=request.getServletContext.getRePath("/WEB-INF/resources");

2.获得工厂

3.通过servletFileUpLoad 解析返回一个List<FileItem>集合

4.进行遍历  根据isFiled 判断是普通文本,然后进行遍历,

5.获得文件输入流,和输入流

6.文件上传成功

代码

 package jquery;

 import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; @WebServlet("/upload")
public class UploadServlet extends HttpServlet{ @Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得上传路径
String savePath=this.getServletContext().getRealPath("/resources"); File file=new File(savePath);
if(!file.isDirectory()&&!file.exists()){
file.mkdir();
} //文件上传 //创建工厂
DiskFileItemFactory factory=new DiskFileItemFactory(); //创建解析器
ServletFileUpload upload=new ServletFileUpload(factory); //解决上传文件乱码问题
upload.setHeaderEncoding("UTF-8"); //判断提交过来的是不是表单数据
if(!upload.isMultipartContent(request)){
return;
} //使用servletFileUpLoad 将解析结果返回List<FileItem> 结合中FileItem对应表单输入项 try {
List<FileItem> item=upload.parseRequest(request);
for (FileItem fileItem : item) {
String message="";
//如果不是file类型
if(fileItem.isFormField()){
System.out.println(fileItem.getString());
}else{
//是上传文件
//得到上传文件名
String filename=fileItem.getName();
System.out.println(filename);
if(filename==null||fileItem.equals("")){
continue;
}
System.out.println(filename);
//获得item 文件输入流
InputStream in=fileItem.getInputStream();
//创建文件输出流
OutputStream out=new FileOutputStream(savePath+"\\"+filename);
System.out.println(savePath+"\\"+filename);
//创建缓冲区
byte[] buffer=new byte[1024];
int len=0;
while((len=in.read(buffer))!=-1){
out.write(buffer, 0, len);
}
in.close();
out.close();
fileItem.delete();
System.out.println("上传成功");
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} }
}

uploadServlet

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-1.9.1.min.js"></script>
</head>
<body> <form action="${pageContext.request.contextPath }/upload" method="post"
enctype="multipart/form-data" method="post">
上传用户名:<input type="text" name="username" /><br /> <input id="file1"
type="file" name="file1" /><img src="c/fakepath/1.jpg"> <input
type="file" name="file2" /> <input type="submit" value="上传" />
</form>
</body>
<script type="text/javascript">
</script>
</html>

html

上一篇:Storm UI说明


下一篇:Servlet 文件上传