javaWeb实战教程5-fileupload文件上传
来源:互联网 发布:软考网络规划师论文 编辑:程序博客网 时间:2024/06/14 07:14
javaWeb实战教程
2.8 commons-fileupload文件上传
Servlet 可以与 HTML form 标签一起使用,来允许用户上传文件到服务器。上传的文件可以是文本文件或图像文件或任何文档。需要引入的 jar 文件:commons-fileupload-1.3.2
、commons-io-2.4.jar
。
在网站根目录WebContent下新建html:lesson8_fileupload.html,写入一个文件上传的表单:
<form action="Lesson8FileUpload" method="post" enctype="multipart/form-data"> 文件1:<input type="file" name="uploadFile1" /><br/> 文件2:<input type="file" name="uploadFile2" /><br/> 姓名:<input type="text" name="username" /><br/> 年龄:<input type="text" name="age" /><br/> <input type="submit" value="上传" /><br/></form>
在cn.funsoftware.Lesson.servlet新建一个类Lesson8FileUpload.java:
@WebServlet("/Lesson8FileUpload")public class Lesson8FileUpload extends HttpServlet { @Override protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}
先判断提交的表单是不是文件上传类型:
ServletFileUpload.isMultipartContent(request)//判断表单是否是文件上传
配置上传参数:
DiskFileItemFactory factory = new DiskFileItemFactory();// 配置上传参数
新建ServletFileUpload
:
ServletFileUpload fileUpload = new ServletFileUpload(factory);fileUpload.setHeaderEncoding("utf-8");// 设置编码
将request
交给ServletFileUpload
处理,获得表单数据:
List<FileItem> fileItems = fileUpload.parseRequest(request);
循环遍历fileItems
里的元素,通过fileItem.isFormField()
方法判断参数是文本还是文件,文本的话可以通过getFieldName()
来获得文本名称,getString()
获得文本内容;如果是文件的话,可以使用write()
方法写入文件中。
完整的文件上传代码写在doPost方法内:
if (!ServletFileUpload.isMultipartContent(request)) { response.getOutputStream().write("不是文件上传表单".getBytes()); return;}// 配置上传参数DiskFileItemFactory factory = new DiskFileItemFactory();ServletFileUpload fileUpload = new ServletFileUpload(factory);fileUpload.setHeaderEncoding("utf-8");// 设置编码try { List<FileItem> fileItems = fileUpload.parseRequest(request); if (fileItems == null) { response.getOutputStream().write("表单里没有上传任何文件".getBytes()); return; } for (FileItem fileItem : fileItems) { if (fileItem.isFormField()) {// 是文本 System.out.println(fileItem.getFieldName() + ":" + fileItem.getString()); } else {// 是文件 String uploadPath = request.getServletContext().getRealPath("./") + File.separator + "upload"; // 如果目录不存在则创建 File uploadDir = new File(uploadPath); if (!uploadDir.exists()) { uploadDir.mkdir(); } String fileName = new File(fileItem.getName()).getName(); String filePath = uploadPath + File.separator + fileName; File storeFile = new File(filePath); // 在控制台输出文件的上传路径 System.out.println(filePath); // 保存文件到硬盘 fileItem.write(storeFile); System.out.println("文件"+fileItem.getFieldName()+"上传成功"); } }} catch (Exception e) { e.printStackTrace();}
阅读全文
0 0
- javaWeb实战教程5-fileupload文件上传
- javaweb 文件上传(fileupload) 下载
- javaweb 文件上传(fileupload) 下载
- javaweb FileUpload 图片文件上传示例
- javaweb中运用fileupload上传文件
- javaweb中的文件上传之fileupload组件
- Java文件上传组件 common-fileUpload 使用教程
- 文件上传控件(FileUpload)
- 使用FileUpload上传文件
- common-fileupload上传文件
- java fileupload上传文件
- FileUpload的文件上传
- 文件上传 FileUpload
- FileUpload上传读取文件
- FileUpload(文件上传)
- 文件上传 commons-FileUpload
- extjs 上传文件 fileUpload
- common-fileupload上传文件
- HDU3790 最短路径问题
- javaWeb实战教程3-过滤器filter
- Leetcode 376. Wiggle Subsequence
- javaWeb实战教程4-jdbc连接数据库和junit单元测试
- 记录MySQL遇到的错误
- javaWeb实战教程5-fileupload文件上传
- PAT 1054 求数组中数量超过一半的数字
- dubbo assembly打包
- 《数据结构学习与实验指导》4-7:修理牧场
- 数据库索引
- javaWeb实战教程6-jsp和jstl
- XCode编译报错 Command /usr/bin/codesign failed with exit code 1
- hdu 2616 Kill the monster(dfs)
- javaWeb实战教程7-搭建项目框架