文件上传小实例

项目结构:

文件上传小实例


编写上传页面:form2.jsp

<%@ 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>
</head>
<body>
<h1>表单二</h1>
<form method="post" action="/upload/Upload2" id="form1" enctype="multipart/form-data">
姓名:<input type="text" name="name"><br>
图片:<input type="file" name="image"><br>
<input type="submit" value="上传">


</form>
</body>
</html>



编写servlet:Upload2.java

package cn.xh.upload.servlet;


import java.io.File;
import java.io.IOException;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


/**
 * Servlet implementation class Upload2
 */
public class Upload2 extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Upload2() {
        super();
        // TODO Auto-generated constructor stub
    }


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");

/*
* 上传三步
* 1. 得到工厂
* 2. 通过工厂创建解析器
* 3. 解析request,得到FileItem集合
* 4. 遍历FileItem集合,调用其API完成文件的保存
*/
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
try {
List<FileItem> fileItemList = sfu.parseRequest(request);
FileItem fi1 = fileItemList.get(0);
FileItem fi2 = fileItemList.get(1);

System.out.println("普通表单项演示:" );

System.out.println("文件名:"+ fi1.getFieldName());
System.out.println("文件内容:"+ fi1.getString("UTF-8"));
System.out.println("文件表单项演示:");
System.out.println("Content-Type: " + fi2.getContentType());
System.out.println("size: " + fi2.getSize());
System.out.println("filename: " + fi2.getName());

// 保存文件
File destFile = new File("E:/beijing.jpg");
fi2.write(destFile);
} catch (FileUploadException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}


}

注意导包(spring下有)



xml:(会自动配置)

<servlet>
    <description></description>
    <display-name>Upload2</display-name>
    <servlet-name>Upload2</servlet-name>
    <servlet-class>cn.xh.upload.servlet.Upload2</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Upload2</servlet-name>
    <url-pattern>/Upload2</url-pattern>
  </servlet-mapping>



上一篇:文件下载小实例


下一篇:python 学习笔记1--TCP客户端