• 最简单的文件上传


     

    1、在做之前需要导入的jar包组件:

    commons-fileupload-1.2.1.jar、commons-io-2.0.jar(依赖包)

    2、页面代码html>  

    <%@ 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>
    	<form action="simpleFileupload" method="post" enctype="multipart/form-data">
    	最简单的文件上传:<input type="file" name="fileupload"/>
    	描述:<input type="text" name="desc"/>
    	<input type="submit" value="submit"/>
    	</form>
    </body>
    </html>
    注意:这里的enctype(编码类型)是“multipart/form-data”

    3、servlet代码

    1. package com.xiaojie.simpleFileupload;  
    2.   
    3. import java.io.File;  
    4. import java.io.FileOutputStream;  
    5. import java.io.IOException;  
    6. import java.io.InputStream;  
    7. import java.io.OutputStream;  
    8. import java.util.List;  
    9.   
    10. import javax.servlet.ServletException;  
    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.   
    16. import org.apache.commons.fileupload.FileItem;  
    17. import org.apache.commons.fileupload.FileUploadException;  
    18. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
    19. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
    20.   
    21. /** 
    22.  * Servlet implementation class SimpleFileupload 
    23.  */  
    24. @WebServlet("/simpleFileupload")  
    25. public class SimpleFileupload extends HttpServlet {  
    26.     private static final long serialVersionUID = 1L;  
    27.   
    28.     /** 
    29.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    30.      */  
    31.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    32.         request.setCharacterEncoding("utf-8");  
    33.         response.setCharacterEncoding("utf-8");  
    34.         //1、创建一个DiskFileItemFactory工厂  
    35.         DiskFileItemFactory factory = new DiskFileItemFactory();  
    36.         //2、创建一个文件上传解析器  
    37.         ServletFileUpload upload = new ServletFileUpload(factory);  
    38.         //解决上传文件名的中文乱码  
    39.         upload.setHeaderEncoding("UTF-8");   
    40.         factory.setSizeThreshold(1024 * 500);//设置内存的临界值为500K  
    41.         File linshi = new File("E:\linshi");//当超过500K的时候,存到一个临时文件夹中  
    42.         factory.setRepository(linshi);  
    43.         upload.setSizeMax(1024 * 1024 * 5);//设置上传的文件总的大小不能超过5M  
    44.         try {  
    45.             // 1. 得到 FileItem 的集合 items  
    46.             List<FileItem> /* FileItem */items = upload.parseRequest(request);  
    47.   
    48.             // 2. 遍历 items:  
    49.             for (FileItem item : items) {  
    50.                 // 若是一个一般的表单域, 打印信息  
    51.                 if (item.isFormField()) {  
    52.                     String name = item.getFieldName();  
    53.                     String value = item.getString("utf-8");  
    54.   
    55.                     System.out.println(name + ": " + value);  
    56.                       
    57.                       
    58.                 }  
    59.                 // 若是文件域则把文件保存到 e:\files 目录下.  
    60.                 else {  
    61.                     String fileName = item.getName();  
    62.                     long sizeInBytes = item.getSize();  
    63.                     System.out.println(fileName);  
    64.                     System.out.println(sizeInBytes);  
    65.   
    66.                     InputStream in = item.getInputStream();  
    67.                     byte[] buffer = new byte[1024];  
    68.                     int len = 0;  
    69.   
    70.                     fileName = "e:\files\" + fileName;//文件最终上传的位置  
    71.                     System.out.println(fileName);  
    72.                     OutputStream out = new FileOutputStream(fileName);  
    73.   
    74.                     while ((len = in.read(buffer)) != -1) {  
    75.                         out.write(buffer, 0, len);  
    76.                     }  
    77.   
    78.                     out.close();  
    79.                     in.close();  
    80.                 }  
    81.             }  
    82.   
    83.         } catch (FileUploadException e) {  
    84.             e.printStackTrace();  
    85.         }  
    86.   
    87.     }  
    88. }  
  • 相关阅读:
    DOM操作之获取HTML、文本和值
    DOM操作之属性和样式操作
    DOM节点的增删改查
    其他选择器
    属性过滤选择器
    Linux
    Linux
    Appium自动化(3)
    Appium自动化(2)
    Appium自动化(1)
  • 原文地址:https://www.cnblogs.com/HHR-SUN/p/6816969.html
Copyright © 2020-2023  润新知