激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Apache Commons fileUpload文件上傳多個(gè)示例分享

Apache Commons fileUpload文件上傳多個(gè)示例分享

2020-06-23 11:44zhangjunhd JAVA教程

這篇文章主要為大家分享了Apache Commons fileUpload文件上傳4個(gè)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文通過實(shí)例來介紹如何使用commons-fileupload.jar,Apache的commons-fileupload.jar可方便的實(shí)現(xiàn)文件的上傳功能,具體內(nèi)容如下

將Apache的commons-fileupload.jar放在應(yīng)用程序的WEB-INF\lib下,即可使用。下面舉例介紹如何使用它的文件上傳功能。

所使用的fileUpload版本為1.2,環(huán)境為Eclipse3.3+MyEclipse6.0。FileUpload 是基于 Commons IO的,所以在進(jìn)入項(xiàng)目前先確定Commons IO的jar包(本文使用commons-io-1.3.2.jar)在WEB-INF\lib下。
此文作示例工程可在文章最后的附件中下載。

示例1

最簡單的例子,通過ServletFileUpload靜態(tài)類來解析Request,工廠類FileItemFactory會(huì)對(duì)mulipart類的表單中的所有字段進(jìn)行處理,不只是file字段。getName()得到文件名,getString()得到表單數(shù)據(jù)內(nèi)容,isFormField()可判斷是否為普通的表單項(xiàng)。
demo1.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  <title>File upload</title>
</head>
<body>
    //必須是multipart的表單數(shù)據(jù)。
  <form name="myform" action="demo1.jsp" method="post"
    enctype="multipart/form-data">
    Your name: <br>
    <input type="text" name="name" size="15"><br>
    File:<br>
    <input type="file" name="myfile"><br>
    <br>
    <input type="submit" name="submit" value="Commit">
  </form>
</body>
</html>

demo1.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<%@ page language="java" contentType="text/html; charset=GB18030"
  pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.util.*"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
  boolean isMultipart = ServletFileUpload.isMultipartContent(request);//檢查輸入請(qǐng)求是否為multipart表單數(shù)據(jù)。
  if (isMultipart == true) {
    FileItemFactory factory = new DiskFileItemFactory();//為該請(qǐng)求創(chuàng)建一個(gè)DiskFileItemFactory對(duì)象,通過它來解析請(qǐng)求。執(zhí)行解析后,所有的表單項(xiàng)目都保存在一個(gè)List中。
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);
    Iterator<FileItem> itr = items.iterator();
    while (itr.hasNext()) {
      FileItem item = (FileItem) itr.next();
      //檢查當(dāng)前項(xiàng)目是普通表單項(xiàng)目還是上傳文件。
      if (item.isFormField()) {//如果是普通表單項(xiàng)目,顯示表單內(nèi)容。
    String fieldName = item.getFieldName();
    if (fieldName.equals("name")) //對(duì)應(yīng)demo1.html中type="text" name="name"
      out.print("the field name is" + item.getString());//顯示表單內(nèi)容。
    out.print("<br>");
      } else {//如果是上傳文件,顯示文件名。
    out.print("the upload file name is" + item.getName());
    out.print("<br>");
      }
    }
  } else {
    out.print("the enctype must be multipart/form-data");
  }
%>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  <title>File upload</title>
</head>
<body>
</body>
</html>

結(jié)果:
the field name isjeff
the upload file name isD:\C語言考試樣題\作業(yè)題.rar

示例2

上傳兩個(gè)文件到指定的目錄。

demo2.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  <title>File upload</title>
</head>
<body>
  <form name="myform" action="demo2.jsp" method="post"
    enctype="multipart/form-data">
    File1:<br>
    <input type="file" name="myfile"><br>
    File2:<br>
    <input type="file" name="myfile"><br>
    <br>
    <input type="submit" name="submit" value="Commit">
  </form>
</body>
</html>

demo2.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<%@ page language="java" contentType="text/html; charset=GB18030"
  pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%String uploadPath="D:\\temp";
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 if(isMultipart==true){
   try{
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);//得到所有的文件
    Iterator<FileItem> itr = items.iterator();
    while(itr.hasNext()){//依次處理每個(gè)文件
     FileItem item=(FileItem)itr.next();
     String fileName=item.getName();//獲得文件名,包括路徑
     if(fileName!=null){
       File fullFile=new File(item.getName());
       File savedFile=new File(uploadPath,fullFile.getName());
       item.write(savedFile);
     }
    }
    out.print("upload succeed");
   }
   catch(Exception e){
     e.printStackTrace();
   }
 }
 else{
   out.println("the enctype must be multipart/form-data");
 }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
</body>
</html>

結(jié)果:
upload succeed

此時(shí),在"D:\temp"下可以看到你上傳的兩個(gè)文件。

示例3

上傳一個(gè)文件到指定的目錄,并限定文件大小。

demo3.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  <title>File upload</title>
</head>
<body>
  <form name="myform" action="demo3.jsp" method="post"
    enctype="multipart/form-data">
    File:<br>
    <input type="file" name="myfile"><br>
    <br>
    <input type="submit" name="submit" value="Commit">
  </form>
</body>
</html>

demo3.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<%@ page language="java" contentType="text/html; charset=GB18030"
  pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
  File uploadPath = new File("D:\\temp");//上傳文件目錄
  if (!uploadPath.exists()) {
    uploadPath.mkdirs();
  }
  // 臨時(shí)文件目錄
  File tempPathFile = new File("d:\\temp\\buffer\\");
  if (!tempPathFile.exists()) {
    tempPathFile.mkdirs();
  }
  try {
    // Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();
 
    // Set factory constraints
    factory.setSizeThreshold(4096); // 設(shè)置緩沖區(qū)大小,這里是4kb
    factory.setRepository(tempPathFile);//設(shè)置緩沖區(qū)目錄
 
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
 
    // Set overall request size constraint
    upload.setSizeMax(4194304); // 設(shè)置最大文件尺寸,這里是4MB
 
    List<FileItem> items = upload.parseRequest(request);//得到所有的文件
    Iterator<FileItem> i = items.iterator();
    while (i.hasNext()) {
      FileItem fi = (FileItem) i.next();
      String fileName = fi.getName();
      if (fileName != null) {
    File fullFile = new File(fi.getName());
    File savedFile = new File(uploadPath, fullFile
       .getName());
    fi.write(savedFile);
      }
    }
    out.print("upload succeed");
  } catch (Exception e) {
    e.printStackTrace();
  }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
</body>
</html>

示例4

利用Servlet來實(shí)現(xiàn)文件上傳。

Upload.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.zj.sample;
 
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
 
@SuppressWarnings("serial")
public class Upload extends HttpServlet {
  private String uploadPath = "D:\\temp"; // 上傳文件的目錄
  private String tempPath = "d:\\temp\\buffer\\"; // 臨時(shí)文件目錄
  File tempPathFile;
 
  @SuppressWarnings("unchecked")
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    try {
      // Create a factory for disk-based file items
      DiskFileItemFactory factory = new DiskFileItemFactory();
 
      // Set factory constraints
      factory.setSizeThreshold(4096); // 設(shè)置緩沖區(qū)大小,這里是4kb
      factory.setRepository(tempPathFile);// 設(shè)置緩沖區(qū)目錄
 
      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory);
 
      // Set overall request size constraint
      upload.setSizeMax(4194304); // 設(shè)置最大文件尺寸,這里是4MB
 
      List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
      Iterator<FileItem> i = items.iterator();
      while (i.hasNext()) {
       FileItem fi = (FileItem) i.next();
       String fileName = fi.getName();
       if (fileName != null) {
         File fullFile = new File(fi.getName());
         File savedFile = new File(uploadPath, fullFile.getName());
         fi.write(savedFile);
       }
      }
      System.out.print("upload succeed");
    } catch (Exception e) {
      // 可以跳轉(zhuǎn)出錯(cuò)頁面
      e.printStackTrace();
    }
  }
 
  public void init() throws ServletException {
    File uploadFile = new File(uploadPath);
    if (!uploadFile.exists()) {
      uploadFile.mkdirs();
    }
    File tempPathFile = new File(tempPath);
    if (!tempPathFile.exists()) {
      tempPathFile.mkdirs();
    }
  }
}

 demo4.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  <title>File upload</title>
</head>
<body>
// action="fileupload"對(duì)應(yīng)web.xml中<servlet-mapping>中<url-pattern>的設(shè)置.
  <form name="myform" action="fileupload" method="post"
    enctype="multipart/form-data">
    File:<br>
    <input type="file" name="myfile"><br>
    <br>
    <input type="submit" name="submit" value="Commit">
  </form>
</body>
</html>

web.xml

?
1
2
3
4
5
6
7
8
9
<servlet>
  <servlet-name>Upload</servlet-name>
  <servlet-class>com.zj.sample.Upload</servlet-class>
</servlet>
 
<servlet-mapping>
  <servlet-name>Upload</servlet-name>
  <url-pattern>/fileupload</url-pattern>
</servlet-mapping>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://zhangjunhd.blog.51cto.com/113473/18331/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品99久久久久久久 | 久久精品一区二区三区四区五区 | 综合激情网 | 男人的天堂视频网站 | 男女无遮挡羞羞视频 | 欧美一级特级 | 精品国产一区二区三区久久久蜜月 | 人人看人人舔 | 99一区二区三区 | 欧美久久久一区二区三区 | 亚洲精品无码不卡在线播放he | 一区二区三区视频播放 | 2018亚洲男人天堂 | 国产成人精品区一区二区不卡 | 91久久综合| 男女无遮挡羞羞视频 | 国产精品一区网站 | 91午夜视频 | 日韩美香港a一级毛片 | 主人在调教室性调教女仆游戏 | 少妇色诱麻豆色哟哟 | 极品一级片 | 99在线精品视频免费观看20 | 中文字幕网站在线 | 人人舔人人射 | 丁香天堂网 | 午夜精品成人一区二区 | 91一区二区三区久久久久国产乱 | 久色乳综合思思在线视频 | av中文在线观看 | 韩日黄色片 | 欧美性生活视频免费 | 成人午夜网址 | 亚洲欧美在线看 | 丰满年轻岳中文字幕一区二区 | 久久久久久久久久性 | 日韩剧情片 | 久久日本| 国产精品久久久久久久久粉嫩 | 日韩精品中文字幕一区二区 | 情侣啪啪网站 |