要实现文件上传功能,我们需要依靠Apache组织的Commons-io.jar,
Commons-fileupload.jar两个jar包来实现,(有许多版本,我给大家仅提供一个版本)或者去官网下载最新版本:http://commons.apache.org/;
链接:https://pan.baidu.com/s/1ImnYdtOavU0ETr7exf4JMg
提取码:npld
1.创建一个fileupdate项目,将其放在自己项目的WEB-INF/lib文件夹下,如图:
2.文件上传一般是存在服务器的upload目录下,文件名陈存在数据库中,为了防止上传文件重名,一般我们用UUID,(绝不会重复)
3.index.jsp页面的代码如下:
注意:form表单不能忘了enctype=“multipart/form-data” 此属性
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<div align="center">
<form action="UploadServlet" enctype="multipart/form-data" method="post">
名称:<input name="name" /> <br>
上传文件:<input name="img" type="file"/><br>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
</div>
</body>
</html>
message.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传文件成功</title>
</head>
<body>
<h1 align="center">上传文件成功!</h1>
</body>
</html>
4.UploadServlet代码:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
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.FileUtils;
public class UploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
DiskFileItemFactory sf= new DiskFileItemFactory();//实例化磁盘被文件列表工厂
String path = request.getRealPath("/upload");//得到上传文件的存放目录
sf.setRepository(new File(path));//设置文件存放目录
sf.setSizeThreshold(1024*1024);//设置文件上传小于1M放在内存中
String rename = "";//文件新生成的文件名
String fileName = "";//文件原名称
String name = "";//普通field字段
//从工厂得到servletupload文件上传类
ServletFileUpload sfu = new ServletFileUpload(sf);
try {
List<FileItem> lst = sfu.parseRequest(request);//得到request中所有的元素
for (FileItem fileItem : lst) {
if(fileItem.isFormField()){
if("name".equals(fileItem.getFieldName())){
name = fileItem.getString("UTF-8");
}
}else{
//获得文件名称
fileName = fileItem.getName();
fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
String houzhui = fileName.substring(fileName.lastIndexOf("."));
rename = UUID.randomUUID()+houzhui;
fileItem.write(new File(path, rename));
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("message.jsp");
out.flush();
out.close();
}
}
一般当你createservlet是web.xml自动配置了相关信息,如图:
运行效果如下:
目前Tomcat下面为空:
选择一张图片,如下:
OK!
自己刚开始运营公众号啦,本人目前就职于某bank,感兴趣的可关注我的公众号,欢迎交流!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/96929.html