使用commons-fileupload.
1.导入jar包(链接:commons-fileupload-1.2.1.jar密码: q9p6)
commons-fileupload-1.2.1.jar 文件上传
commons-io-1.4.jar 它是提供的io工具.
介绍commons-fileupload
1.DiskFileItemFactory类
2.ServletFileUpload类
3.FileItem
2.快速入门:
1.创建upload2.jsp页面
2.创建Upload2Servlet
1.创建一个DiskFileItemFactory DiskFileItemFactory factory=new DiskFileItemFactory();
2.创建ServletFileUpload类 ServletFileUpload upload=new ServletFileUpload(factory);
3.解析所有上传数据 List<FileItem> items = upload.parseRequest(request);
3.了遍历items集合,集合中的每一项,就是一个上传数据.
1.isFormField();
2.getFieldName(); 返回值String,得到组件名称 <input name="">
3.getName();
返回值是String,得到的是上传文件的名称.
注意:浏览器不同,它们得到的效果不一样。
1.包含全路径名称 例如: C:\Users\Administrator\Desktop\a.txt
2.只包含上传文件名称 例如:a.txt
4.getString();
这个方法可以获取非上传组件的内容,相当于 getParameter方法作用。
如果是上传组件,上传的文件是文本文件,可以获取到文件文件的内容。
但是如果不是文件文件,例如:是一张图片,这样获取合适吗?
5.获取上传文件的内容,保存到服务器端.
item.getInputStream();它是用于读取上传文件内容的输入流.
使用文件复制操作就可以完成文件上传。
IOUtils.copy(item.getInputStream(), fos);
代码实现:
lib中导入两个jar包.
在javaweb文件上传(1)的项目里面添加upload2.jsp页面,还要添加一个Upload2Servlet的servlet文件.
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/upload2" method="post" encType="multipart/form-data">
<input type="text" name="content"><br>
<input type="file" name="f"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
servlet的代码实现.
package cn.itcast.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
public class Upload2Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.创建DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
//2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
//底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
然后运行服务器,输入good,上传a.txt文件,在浏览器运行,抓包得到如下.
servlet中的List<FileItem> 就是这些分割的内容,就相当于得到了这个文本的内容.
然后修改服务器的upload2servlet的代码,如下所示.
package cn.itcast.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import org.apache.commons.io.IOUtils;
public class Upload2Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.创建 DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
// 3.遍历集合
for (FileItem item : items) {
if (item.isFormField()) {
// 这就是得到了<input type="text" name="content">这样的组件
String fieldName = item.getFieldName();
System.out.println(fieldName);
} else {
// 这就是得到了<input type="file"> 这样的组件.
String fieldName = item.getFieldName();
System.out.println("上传组件的名称:" + fieldName);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
此时上传文件,在控制台输出.
String filename = item.getFileName();如下图所示.
接下来再修改upload2servlet.java文件的内容.
package cn.itcast.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import org.apache.commons.io.IOUtils;
public class Upload2Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
// 1.创建 DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
// 3.遍历集合
for (FileItem item : items) {
if (item.isFormField()) {
// 这就是得到了<input type="text" name="content">这样的组件
String fieldName = item.getFieldName();
System.out.println(fieldName);
String name = item.getName();
System.out.println(name);
String msg = item.getString();
System.out.println(msg);
} else {
// 这就是得到了<input type="file"> 这样的组件.
String fieldName = item.getFieldName();
System.out.println("上传组件的名称:" + fieldName);
String name = item.getName(); // 上传文件名称
System.out.println("上传组件:" + name);
String msg = item.getString();
System.out.println(msg);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
// 1.创建 DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
// 3.遍历集合
for (FileItem item : items) {
if (item.isFormField()) {
// 这就是得到了<input type="text" name="content">这样的组件
String fieldName = item.getFieldName();
System.out.println(fieldName);
String name = item.getName();
System.out.println(name);
String msg = item.getString();
System.out.println(msg);
} else {
// 这就是得到了<input type="file"> 这样的组件.
String fieldName = item.getFieldName();
System.out.println("上传组件的名称:" + fieldName);
String name = item.getName(); // 上传文件名称
System.out.println("上传组件:" + name);
String msg = item.getString();
System.out.println(msg);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
此时控制台打印输出.
结构图分析得到,getFileName(); good 0123456789 getString();.
getString();
这个方法可以获取非上传组件的内容,相当于 getParameter方法作用。
如果是上传组件,上传的文件是文本文件,可以获取到文件文件的内容。
但是如果不是文件文件,例如:是一张图片,这样获取合适吗?
获取上传文件的内容,保存到服务器端.
item.getInputStream();它是用于读取上传文件内容的输入流.
使用文件复制操作就可以完成文件上传。
IOUtils.copy(item.getInputStream(), fos);
故修改upload2servlet.java类的文件.
package cn.itcast.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import org.apache.commons.io.IOUtils;
public class Upload2Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.创建 DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
// 3.遍历集合
for (FileItem item : items) {
if (item.isFormField()) {
// 这就是得到了<input type="text" name="content">这样的组件
// String fieldName = item.getFieldName();
// System.out.println(fieldName);
//
// String name = item.getName();
// System.out.println(name);
//
// String msg = item.getString();
// System.out.println(msg);
} else {
// 这就是得到了<input type="file"> 这样的组件.
// String fieldName = item.getFieldName();
// System.out.println("上传组件的名称:" + fieldName);
//
//String name = item.getName(); // 上传文件名称
// System.out.println("上传组件:" + name);
//
// String msg = item.getString();
// System.out.println(msg);
// 获取上传文件内容
InputStream is = item.getInputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = is.read(b)) != -1) {
System.out.print(new String(b,0,len));
}
is.close();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.创建 DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
// 3.遍历集合
for (FileItem item : items) {
if (item.isFormField()) {
// 这就是得到了<input type="text" name="content">这样的组件
// String fieldName = item.getFieldName();
// System.out.println(fieldName);
//
// String name = item.getName();
// System.out.println(name);
//
// String msg = item.getString();
// System.out.println(msg);
} else {
// 这就是得到了<input type="file"> 这样的组件.
// String fieldName = item.getFieldName();
// System.out.println("上传组件的名称:" + fieldName);
//
//String name = item.getName(); // 上传文件名称
// System.out.println("上传组件:" + name);
//
// String msg = item.getString();
// System.out.println(msg);
// 获取上传文件内容
InputStream is = item.getInputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = is.read(b)) != -1) {
System.out.print(new String(b,0,len));
}
is.close();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
控制台打印输出得到.
若我此时上传一张图片,控制台则会打印出乱码.
但是我们最终的目的是复制文件,而不是打印到控制台输出所以修改一下upload2servlet.java的代码.
package cn.itcast.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import org.apache.commons.io.IOUtils;
public class Upload2Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.创建 DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
// 3.遍历集合
for (FileItem item : items) {
if (item.isFormField()) {
// 这就是得到了<input type="text" name="content">这样的组件
// String fieldName = item.getFieldName();
// System.out.println(fieldName);
//
// String name = item.getName();
// System.out.println(name);
//
// String msg = item.getString();
// System.out.println(msg);
} else {
// 这就是得到了<input type="file"> 这样的组件.
// String fieldName = item.getFieldName();
// System.out.println("上传组件的名称:" + fieldName);
//
String name = item.getName(); // 上传文件名称,但是包含了全路径,所以截取出来,如下代码.
name = name.substring(name.lastIndexOf("\\") + 1);
// System.out.println("上传组件:" + name);
//
// String msg = item.getString();
// System.out.println(msg);
// 获取上传文件内容,完成文件 上传操作
InputStream is = item.getInputStream();
byte[] b = new byte[1024];
int len = -1;
FileOutputStream fos = new FileOutputStream("d:/upload/" + name);
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.close();
is.close();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
然后上传文件后就可以将文件复制到指定的目录下面了 : d:/upload/
因为我们前面导入了commons-io.jar的包,所以可以简化一些代码,直接用里面的类.
package cn.itcast.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import org.apache.commons.io.IOUtils;
public class Upload2Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.创建 DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
// 3.遍历集合
for (FileItem item : items) {
if (item.isFormField()) {
// 这就是得到了<input type="text" name="content">这样的组件
// String fieldName = item.getFieldName();
// System.out.println(fieldName);
//
// String name = item.getName();
// System.out.println(name);
//
// String msg = item.getString();
// System.out.println(msg);
} else {
// 这就是得到了<input type="file"> 这样的组件.
// String fieldName = item.getFieldName();
// System.out.println("上传组件的名称:" + fieldName);
//
String name = item.getName(); // 上传文件名称
System.out.println(name);
name = name.substring(name.lastIndexOf("\\") + 1);
// System.out.println("上传组件:" + name);
//
// String msg = item.getString();
// System.out.println(msg);
// 获取上传文件内容,完成文件 上传操作
// InputStream is = item.getInputStream();
// byte[] b = new byte[1024];
// int len = -1;
FileOutputStream fos = new FileOutputStream("d:/upload/" + name);
// while ((len = is.read(b)) != -1) {
//
// fos.write(b, 0, len);
//
// }
// fos.close();
// is.close();
IOUtils.copy(item.getInputStream(), fos);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
也可以将上传的文件直接复制到了,指定的目录中.