文件上传——servlet实现

自己对照别的博主的博客实现的,记录用。

整个上传的结构如下:

文件上传——servlet实现

上传的页面:unload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>文件上传</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form action="<%=path %>/Servlet/UploadHandleServlet" enctype="multipart/form-data" method="post">
上传用户:<input type="text" name="username"><br>
上传文件1:<input type="file" name="file1"><br>
上传文件2:<input type="file" name="file2"><br>
<input type="submit" value="提交"> </form>
</body>
</html>

提示上传信息message.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>提示消息</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
${message}
</body>
</html>

处理上传的servlet

package Servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; public class UploadHandleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String savepath=this.getServletContext().getRealPath("/WEB-INF/upload");
File file=new File(savepath);
System.out.println(savepath);
if(!file.exists() && !file.isDirectory())
{
System.out.println(savepath+"目录不存在创建");
file.mkdir();
} String message=""; try{
System.out.println("1");
DiskFileItemFactory fctory=new DiskFileItemFactory();
ServletFileUpload fileuplaod=new ServletFileUpload(fctory);
fileuplaod.setHeaderEncoding("UTF-8");
if(!ServletFileUpload.isMultipartContent(request)){
System.out.println("5");
return;
}
System.out.println("6");
List<FileItem> list=fileuplaod.parseRequest(request);
for(FileItem item : list){
if(item.isFormField()){
System.out.println("2");
String name=item.getFieldName();
String value=item.getString("UTF-8");
System.out.println(name+"="+value); }else{
System.out.println("3");
String name=item.getName();
System.out.println(name);
if(name==null||name.trim().equals("")){
continue;
}
name=name.substring(name.lastIndexOf("\\")+1);
InputStream in=item.getInputStream();
FileOutputStream out =new FileOutputStream(savepath+"\\"+name);
byte buffer[]=new byte[1024];
int len=0;
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);;
}
System.out.println("4");
in.close();
out.close();
item.delete();
message="文件上传成功"; } } }catch(Exception e){
message="文件上传失败!";
e.printStackTrace(); }
request.setAttribute("message", message);
request.getRequestDispatcher("../message.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request,response);
}
}

映射:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>UploadHandleServlet</servlet-name>
<servlet-class>Servlet.UploadHandleServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>UploadHandleServlet</servlet-name>
<url-pattern>/Servlet/UploadHandleServlet</url-pattern>
</servlet-mapping> </web-app>

上传结果:

文件上传——servlet实现

这里需要注意:

if(!ServletFileUpload.isMultipartContent(request)){
return;
}

判断上传的类型,在unload.jsp form表单定类型

enctype="multipart/form-data"

file1和file2是在获取上传文件的名字是出错了才出现的结果,getFiledName获取普通输入项,如上传的用户名, getName获取上传的文件名称

String name=item.getFieldName();
上一篇:android sdk manager 国内镜像


下一篇:Examples_07_06 无法下载android的sdk