标签:isp 复制 encoding inf 最简 ons show factor null
|--实现文件上传的方式
1、进入http://commons.apache.org/proper/commons-fileupload/using.html
2、点击用户指南
3、对着用户指南一步步复制粘贴,并把代码撸成自己想要的样子
补充:记得要将commons-io-2.4.jar和commons-fileupload-1.2.2.jar这两个包放到编译器里面
|--现成的代码
1 <%-- 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2019/10/8 5 Time: 16:14 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>作业三第1遍</title> 12 </head> 13 <body> 14 文件上传注意事项:<br> 15 <ol> 16 <li>提交方式必须是post</li> 17 <li>enctype="multipart/form-data"是以字节数据的形式提交表单,否则默认是kv形式,只会提交文件名</li> 18 <li>如果以默认的application/x-www-form-urlencode的方式提交,上传的就不是文件本身</li> 19 </ol> 20 <%--application/x-www-form-urlencoded 以键值对的方式上传 --%> 21 <%-- multipart/form-data 以二进制的方式上传 --%> 22 <form action="/work/UploadServlet" method="post" enctype="multipart/form-data"> 23 <input type="text" name="data1" value="data1Value"> 24 <hr> 25 <input type="text" name="data2" value="data2Value"> 26 <hr> 27 <input type="file" name="dataFile"> 28 <hr> 29 <input type="submit" value="上传该表单"> 30 <hr> 31 </form> 32 33 34 </body> 35 </html>
1 package com.bd25; 2 3 import org.apache.commons.fileupload.FileItem; 4 import org.apache.commons.fileupload.FileItemFactory; 5 import org.apache.commons.fileupload.FileUploadException; 6 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 7 import org.apache.commons.fileupload.servlet.ServletFileUpload; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletInputStream; 11 import javax.servlet.annotation.WebServlet; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 import java.io.File; 16 import java.io.IOException; 17 import java.io.UnsupportedEncodingException; 18 import java.util.Enumeration; 19 import java.util.Iterator; 20 import java.util.List; 21 22 @WebServlet(name = "UploadServlet", urlPatterns = "/UploadServlet") 23 public class UploadServlet extends HttpServlet { 24 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 //处理中文乱码--已经测试这个要不要无所谓 26 request.setCharacterEncoding("UTF-8"); 27 28 //判断是不是传入的一个二进制的流 29 boolean isMultipart = ServletFileUpload.isMultipartContent(request); 30 //创建一个简单工厂实例 31 FileItemFactory factory = new DiskFileItemFactory(); 32 //创建一个新的文件上传处理程序 33 ServletFileUpload upload = new ServletFileUpload(factory); 34 //解析request 35 try { 36 List<FileItem> fileItems = upload.parseRequest(request); 37 //处理上传的项目 38 Iterator iter=fileItems.iterator(); 39 while(iter.hasNext()){ 40 FileItem item = (FileItem) iter.next(); 41 if (item.isFormField()){ 42 //普通域 43 processFormField(item); 44 }else{ 45 //文件域 此处注意processUploadFile()方法的参数里面传入request的目的是让下面写文件的时候获取根目录用的 46 processUploadFile(item,request); 47 } 48 49 } 50 51 } catch (FileUploadException e) { 52 e.printStackTrace(); 53 } 54 55 56 ServletInputStream inputStream = request.getInputStream(); 57 byte[] bs=new byte[1024]; 58 int total=-1; 59 while ((total=inputStream.read(bs))!=-1){ 60 System.out.print(new String(bs,0,total)); 61 } 62 } 63 64 /** 65 * 文件域 66 * @param item 67 * @param request 68 */ 69 private void processUploadFile(FileItem item, HttpServletRequest request) { 70 //获取文件相关信息 71 String fileName=""; 72 if (!item.isFormField()) { 73 String fieldName = item.getFieldName(); 74 fileName = item.getName(); 75 String contentType = item.getContentType(); 76 boolean isInMemory = item.isInMemory(); 77 long sizeInBytes = item.getSize(); 78 System.out.println("文件名: "+fileName+" 文件属性:"+fieldName+" 文件大小:"+sizeInBytes+" 文件类型:"+contentType); 79 } 80 //以下是转存操作,找到储存文件的根路径 81 String fileRootPath=request.getServletContext().getRealPath("/WEB-INF/upload"); 82 File dir = new File(fileRootPath); 83 if (!dir.exists()){ 84 dir.mkdirs(); 85 } 86 //写文件 87 File file = new File(fileRootPath+"/"+fileName); 88 try { 89 item.write(file); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } 93 } 94 95 /** 96 * 普通域 97 * @param item 98 */ 99 private void processFormField(FileItem item) { 100 String key = item.getFieldName(); 101 String value = null; 102 try { 103 //处理中文乱码 104 value = item.getString("UTF-8"); 105 } catch (UnsupportedEncodingException e) { 106 e.printStackTrace(); 107 } 108 System.out.println(key+"----"+value); 109 } 110 111 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 112 113 } 114 115 @Override 116 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 117 super.service(req, resp); 118 System.out.println("刷新...."); 119 } 120 121 @Override 122 public void init() throws ServletException { 123 super.init(); 124 System.out.println("初始化...."); 125 } 126 }
|--运行结果
//说明:路径出现的位置就是网页发布的路径
//说明:路径出现的位置就是网页发布的路径
标签:isp 复制 encoding inf 最简 ons show factor null
原文地址:https://www.cnblogs.com/twuxian/p/11637956.html