国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java FileUploadUtil工具類詳解

Java FileUploadUtil工具類詳解

2020-12-28 09:49Godliness丶 Java教程

這篇文章主要為大家詳細介紹了Java FileUploadUtil工具類的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了FileUploadUtil工具類的具體代碼,供大家參考,具體內容如下

?
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.gootrip.util;
 
import java.io.File;
import java.util.*;
import org.apache.commons.fileupload.*;
import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;
import java.io.IOException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import java.util.regex.Matcher;
 
public class FileUploadUtil {
 
  //當上傳文件超過限制時設定的臨時文件位置,注意是絕對路徑
  private String tempPath = null;
 
  //文件上傳目標目錄,注意是絕對路徑
  private String dstPath = null;
 
  //新文件名稱,不設置時默認為原文件名
  private String newFileName = null;
  //獲取的上傳請求
  private HttpServletRequest fileuploadReq = null;
 
  //設置最多只允許在內存中存儲的數據,單位:字節,這個參數不要設置太大
  private int sizeThreshold = 4096;
 
  //設置允許用戶上傳文件大小,單位:字節
  //共10M
  private long sizeMax = 10485760;
 
  //圖片文件序號
  private int picSeqNo = 1;
 
  private boolean isSmallPic = false;
 
  public FileUploadUtil(){
  }
 
  public FileUploadUtil(String tempPath, String destinationPath){
    this.tempPath = tempPath;
    this.dstPath = destinationPath;
  }
 
  public FileUploadUtil(String tempPath, String destinationPath, HttpServletRequest fileuploadRequest){
    this.tempPath  = tempPath;
    this.dstPath = destinationPath;
    this.fileuploadReq = fileuploadRequest;
  }
 
  /** 文件上載
   * @return true —— success; false —— fail.
   */
  public boolean Upload(){
    DiskFileItemFactory factory = new DiskFileItemFactory();
 
    try {
 
      //如果沒有上傳目的目錄,則創建它
      FileUtil.makeDirectory(dstPath+"/ddd");
      /*if (!FileUtil.makeDirectory(dstPath+"/ddd")) {
        throw new IOException("Create destination Directory Error.");
      }*/
      //如果沒有臨時目錄,則創建它
      FileUtil.makeDirectory(tempPath+"/ddd");
      /*if (!FileUtil.makeDirectory(tempPath+"/ddd")) {
        throw new IOException("Create Temp Directory Error.");
      }*/
 
      //上傳項目只要足夠小,就應該保留在內存里。
      //較大的項目應該被寫在硬盤的臨時文件上。
      //非常大的上傳請求應該避免。
      //限制項目在內存中所占的空間,限制最大的上傳請求,并且設定臨時文件的位置。
 
      //設置最多只允許在內存中存儲的數據,單位:字節
      factory.setSizeThreshold(sizeThreshold);
      // the location for saving data that is larger than getSizeThreshold()
      factory.setRepository(new File(tempPath));
 
      ServletFileUpload upload = new ServletFileUpload(factory);
      //設置允許用戶上傳文件大小,單位:字節
      upload.setSizeMax(sizeMax);
 
      List fileItems = upload.parseRequest(fileuploadReq);
      // assume we know there are two files. The first file is a small
      // text file, the second is unknown and is written to a file on
      // the server
      Iterator iter = fileItems.iterator();
 
      // 正則匹配,過濾路徑取文件名
      String regExp = ".+\\\\(.+)$";
 
      // 過濾掉的文件類型
      String[] errorType = {".exe", ".com", ".cgi", ".asp", ".php", ".jsp"};
      Pattern p = Pattern.compile(regExp);
      while (iter.hasNext()) {
        System.out.println("++00++====="+newFileName);
        FileItem item = (FileItem) iter.next();
        //忽略其他不是文件域的所有表單信息
        if (!item.isFormField()) {
          String name = item.getName();
          System.out.println("++++====="+name);
          long size = item.getSize();
          //有多個文件域時,只上傳有文件的
          if ((name == null || name.equals("")) && size == 0)
            continue;
          Matcher m = p.matcher(name);
          boolean result = m.find();
          if (result) {
            for (int temp = 0; temp < errorType.length; temp++) {
              if (m.group(1).endsWith(errorType[temp])) {
                throw new IOException(name + ": Wrong File Type");
              }
            }
            String ext = "."+FileUtil.getTypePart(name);
            try {
              //保存上傳的文件到指定的目錄
              //在下文中上傳文件至數據庫時,將對這里改寫
              //沒有指定新文件名時以原文件名來命名
              if (newFileName == null || newFileName.trim().equals(""))
              {
                item.write(new File(dstPath +"/"+ m.group(1)));
              }
              else
              {
                String uploadfilename = "";
                if (isSmallPic)
                {
                  uploadfilename = dstPath +"/"+ newFileName+"_"+picSeqNo+"_small"+ext;
                }
                else
                {
                  uploadfilename = dstPath +"/"+ newFileName+"_"+picSeqNo+ext;
                }
                //生成所有未生成的目錄
                System.out.println("++++====="+uploadfilename);
                FileUtil.makeDirectory(uploadfilename);
                //item.write(new File(dstPath +"/"+ newFileName));
                item.write(new File(uploadfilename));
              }
              picSeqNo++;
              //out.print(name + "&nbsp;&nbsp;" + size + "<br>");
            } catch (Exception e) {
              //out.println(e);
              throw new IOException(e.getMessage());
            }
          } else {
            throw new IOException("fail to upload");
          }
        }
      }
    } catch (IOException e) {
      System.out.println(e);
    } catch (FileUploadException e) {
      System.out.println(e);
    }
    return true;
  }
 
  /**從路徑中獲取單獨文件名
   * @author
   *
   * TODO 要更改此生成的類型注釋的模板,請轉至
   * 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
   */
  public String GetFileName(String filepath)
  {
    String returnstr = "*.*";
    int length    = filepath.trim().length();
 
    filepath = filepath.replace('\\', '/');
    if(length >0)
    {
      int i = filepath.lastIndexOf("/");
      if (i >= 0)
      {
        filepath = filepath.substring(i + 1);
        returnstr = filepath;
      }
    }
    return returnstr;
  }
  /**
   * 設置臨時存貯目錄
   */
  public void setTmpPath(String tmppath)
  {
    this.tempPath = tmppath;
  }
  /**
   * 設置目標目錄
   */
  public void setDstPath(String dstpath) {
    this.dstPath = dstpath;
  }
  /**
   * 設置最大上傳文件字節數,不設置時默認10M
   */
  public void setFileMaxSize(long maxsize) {
    this.sizeMax = maxsize;
  }
  /**
   * 設置Http 請求參數,通過這個能數來獲取文件信息
   */
  public void setHttpReq(HttpServletRequest httpreq) {
    this.fileuploadReq = httpreq;
  }
  /**
   * 設置Http 請求參數,通過這個能數來獲取文件信息
   */
  public void setNewFileName(String filename) {
    this.newFileName = filename;
  }
 
  /**
   * 設置此上傳文件是否是縮略圖文件,這個參數主要用于縮略圖命名
   */
  public void setIsSmalPic(boolean isSmallPic) {
    this.isSmallPic = isSmallPic;
  }
 
  /**
   * 設置Http 請求參數,通過這個能數來獲取文件信息
   */
  public void setPicSeqNo(int seqNo) {
    this.picSeqNo = seqNo;
  }
 
 
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/qq_878799579/article/details/77862005

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品久久久久久 | 在线视频a| 黄色毛片免费网站 | 亚洲国内精品 | 精品亚洲一区二区三区 | 国产97在线 | 免费 | 国产精品美女久久久久久久久久久 | 色片在线观看 | 国产精品一区二区免费 | 午夜视频在线观看一区二区三区 | 久久久亚洲国产天美传媒修理工 | 天堂一区 | 午夜爱爱毛片xxxx视频免费看 | 欧美天堂一区 | 一二三区字幕免费观看av | 日本一本视频 | 黄色电影免费在线观看 | 欧美日韩久久精品 | 欧美精品成人一区二区三区四区 | 亚洲精品国产成人 | 久草久| 欧美一级全黄 | 国产成人精品一区二区在线 | 国产精品久久久久久久 | 日本不卡高字幕在线2019 | av在线中文 | 亚洲精品一区二区在线观看 | 一级片在线免费观看视频 | 亚洲视频一区二区三区 | 成人精品在线视频 | 国产一区二区影院 | 精品国产一区二区 | 亚洲精品视频免费在线观看 | 久久成人免费 | 91亚洲精品乱码久久久久久蜜桃 | 亚洲日本网站 | 欧洲精品 | 中文字幕成人在线 | 性片网站 | 久久久久国产精品www | 99视频精品在线 |