最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java FileUploadUtil工具類(lèi)詳解

 更新時(shí)間:2017年09月06日 09:54:45   作者:Godliness丶  
這篇文章主要為大家詳細(xì)介紹了Java FileUploadUtil工具類(lèi)的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了FileUploadUtil工具類(lèi)的具體代碼,供大家參考,具體內(nèi)容如下

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 {

  //當(dāng)上傳文件超過(guò)限制時(shí)設(shè)定的臨時(shí)文件位置,注意是絕對(duì)路徑
  private String tempPath = null;

  //文件上傳目標(biāo)目錄,注意是絕對(duì)路徑
  private String dstPath = null;

  //新文件名稱(chēng),不設(shè)置時(shí)默認(rèn)為原文件名
  private String newFileName = null;
  //獲取的上傳請(qǐng)求
  private HttpServletRequest fileuploadReq = null;

  //設(shè)置最多只允許在內(nèi)存中存儲(chǔ)的數(shù)據(jù),單位:字節(jié),這個(gè)參數(shù)不要設(shè)置太大
  private int sizeThreshold = 4096;

  //設(shè)置允許用戶上傳文件大小,單位:字節(jié)
  //共10M
  private long sizeMax = 10485760;

  //圖片文件序號(hào)
  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 {

      //如果沒(méi)有上傳目的目錄,則創(chuàng)建它
      FileUtil.makeDirectory(dstPath+"/ddd");
      /*if (!FileUtil.makeDirectory(dstPath+"/ddd")) {
        throw new IOException("Create destination Directory Error.");
      }*/
      //如果沒(méi)有臨時(shí)目錄,則創(chuàng)建它
      FileUtil.makeDirectory(tempPath+"/ddd");
      /*if (!FileUtil.makeDirectory(tempPath+"/ddd")) {
        throw new IOException("Create Temp Directory Error.");
      }*/

      //上傳項(xiàng)目只要足夠小,就應(yīng)該保留在內(nèi)存里。
      //較大的項(xiàng)目應(yīng)該被寫(xiě)在硬盤(pán)的臨時(shí)文件上。
      //非常大的上傳請(qǐng)求應(yīng)該避免。
      //限制項(xiàng)目在內(nèi)存中所占的空間,限制最大的上傳請(qǐng)求,并且設(shè)定臨時(shí)文件的位置。

      //設(shè)置最多只允許在內(nèi)存中存儲(chǔ)的數(shù)據(jù),單位:字節(jié)
      factory.setSizeThreshold(sizeThreshold);
      // the location for saving data that is larger than getSizeThreshold()
      factory.setRepository(new File(tempPath));

      ServletFileUpload upload = new ServletFileUpload(factory);
      //設(shè)置允許用戶上傳文件大小,單位:字節(jié)
      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();

      // 正則匹配,過(guò)濾路徑取文件名
      String regExp = ".+\\\\(.+)$";

      // 過(guò)濾掉的文件類(lèi)型
      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();
          //有多個(gè)文件域時(shí),只上傳有文件的
          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 {
              //保存上傳的文件到指定的目錄
              //在下文中上傳文件至數(shù)據(jù)庫(kù)時(shí),將對(duì)這里改寫(xiě)
              //沒(méi)有指定新文件名時(shí)以原文件名來(lái)命名
              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;
  }

  /**從路徑中獲取單獨(dú)文件名
   * @author
   *
   * TODO 要更改此生成的類(lèi)型注釋的模板,請(qǐng)轉(zhuǎn)至
   * 窗口 - 首選項(xiàng) - 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;
  }
  /**
   * 設(shè)置臨時(shí)存貯目錄
   */
  public void setTmpPath(String tmppath)
  {
    this.tempPath = tmppath;
  }
  /**
   * 設(shè)置目標(biāo)目錄
   */
  public void setDstPath(String dstpath) {
    this.dstPath = dstpath;
  }
  /**
   * 設(shè)置最大上傳文件字節(jié)數(shù),不設(shè)置時(shí)默認(rèn)10M
   */
  public void setFileMaxSize(long maxsize) {
    this.sizeMax = maxsize;
  }
  /**
   * 設(shè)置Http 請(qǐng)求參數(shù),通過(guò)這個(gè)能數(shù)來(lái)獲取文件信息
   */
  public void setHttpReq(HttpServletRequest httpreq) {
    this.fileuploadReq = httpreq;
  }
  /**
   * 設(shè)置Http 請(qǐng)求參數(shù),通過(guò)這個(gè)能數(shù)來(lái)獲取文件信息
   */
  public void setNewFileName(String filename) {
    this.newFileName = filename;
  }

  /**
   * 設(shè)置此上傳文件是否是縮略圖文件,這個(gè)參數(shù)主要用于縮略圖命名
   */
  public void setIsSmalPic(boolean isSmallPic) {
    this.isSmallPic = isSmallPic;
  }

  /**
   * 設(shè)置Http 請(qǐng)求參數(shù),通過(guò)這個(gè)能數(shù)來(lái)獲取文件信息
   */
  public void setPicSeqNo(int seqNo) {
    this.picSeqNo = seqNo;
  }


}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot調(diào)用DeepSeek接口的實(shí)現(xiàn)

    SpringBoot調(diào)用DeepSeek接口的實(shí)現(xiàn)

    本文主要介紹了SpringBoot調(diào)用DeepSeek接口的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • Springboot中@Transactional注解與異常處理機(jī)制方式

    Springboot中@Transactional注解與異常處理機(jī)制方式

    這篇文章主要介紹了Springboot中@Transactional注解與異常處理機(jī)制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java如何實(shí)現(xiàn)登錄token令牌

    Java如何實(shí)現(xiàn)登錄token令牌

    這篇文章主要介紹了Java如何實(shí)現(xiàn)登錄token令牌,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • java 設(shè)計(jì)模式之適配器模式的詳解

    java 設(shè)計(jì)模式之適配器模式的詳解

    這篇文章主要介紹了java 設(shè)計(jì)模式之適配器模式的詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • SpringBoot升級(jí)到2.7.18后不兼容的地方及解決

    SpringBoot升級(jí)到2.7.18后不兼容的地方及解決

    這篇文章主要介紹了SpringBoot升級(jí)到2.7.18后不兼容的地方及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • mybatis-plus返回map自動(dòng)轉(zhuǎn)駝峰配置操作

    mybatis-plus返回map自動(dòng)轉(zhuǎn)駝峰配置操作

    這篇文章主要介紹了mybatis-plus返回map自動(dòng)轉(zhuǎn)駝峰配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • Spring Boot + thymeleaf 實(shí)現(xiàn)文件上傳下載功能

    Spring Boot + thymeleaf 實(shí)現(xiàn)文件上傳下載功能

    最近同事問(wèn)我有沒(méi)有有關(guān)于技術(shù)的電子書(shū),我打開(kāi)電腦上的小書(shū)庫(kù),但是郵件發(fā)給他太大了,公司又禁止用文件夾共享,于是花半天時(shí)間寫(xiě)了個(gè)小的文件上傳程序,部署在自己的Linux機(jī)器上,需要的朋友可以參考下
    2018-01-01
  • java生成驗(yàn)證碼圖片的方法

    java生成驗(yàn)證碼圖片的方法

    這篇文章主要為大家詳細(xì)介紹了java生成驗(yàn)證碼圖片的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • 使用IDEA打jar包的詳細(xì)圖文教程

    使用IDEA打jar包的詳細(xì)圖文教程

    JAR文件是一種壓縮文件,與常見(jiàn)的ZIP壓縮文件兼容,被稱(chēng)為JAR包,下面這篇文章主要給大家介紹了關(guān)于使用IDEA打jar包的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語(yǔ)言支持操作示例

    Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語(yǔ)言支持操作示例

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語(yǔ)言支持操作,結(jié)合實(shí)例形式分析了Bean定義中的SpEL表達(dá)式語(yǔ)言操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-12-12

最新評(píng)論

榆树市| 石家庄市| 六安市| 收藏| 乌鲁木齐县| 丰镇市| 溧阳市| 南靖县| 石屏县| 三亚市| 邵阳县| 巴彦淖尔市| 赣州市| 沛县| 西宁市| 桐梓县| 蚌埠市| 顺平县| 特克斯县| 东至县| 蓝田县| 罗甸县| 张家口市| 黄龙县| 鄂尔多斯市| 石景山区| 辽宁省| 宾川县| 平和县| 南华县| 达拉特旗| 海宁市| 余庆县| 江川县| 马公市| 孟津县| 灵宝市| 琼海市| 侯马市| 云林县| 库车县|