文件上传小实例

简介:

项目结构:



编写上传页面: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>



目录
相关文章
C#文件上传
C#文件上传
59 0
|
6月前
|
JSON 数据格式
文件上传~~
文件上传~~
44 0
|
3月前
|
前端开发 JavaScript 数据库
多个文件上传
多个文件上传
29 0
|
JavaScript 前端开发 移动开发
浅谈文件上传
浅谈文件上传
浅谈文件上传
|
6月前
|
移动开发 前端开发
VForm3的文件上传方式
VForm3的文件上传方式
171 0
|
6月前
文件上传地址的获取方法
文件上传地址的获取方法
60 1
|
存储 移动开发 JavaScript
|
安全 应用服务中间件 PHP
[SUCTF 2019]CheckIn(文件上传)
[SUCTF 2019]CheckIn(文件上传)
159 0
|
数据安全/隐私保护 Windows
|
JavaScript
你真的了解文件上传吗?
你真的了解文件上传吗?