當(dāng)前位置:首頁 > IT技術(shù) > Web編程 > 正文

PHP大文件上傳進(jìn)度條資源
2021-09-18 17:10:10

?

第一點(diǎn):Java代碼實(shí)現(xiàn)文件上傳

FormFile file = manform.getFile();

String newfileName =?null;

String newpathname =?null;

String fileAddre =?"/numUp";

try

{

????InputStream stream = file.getInputStream();//?把文件讀入

????String filePath = request.getRealPath(fileAddre);//取系統(tǒng)當(dāng)前路徑

????File file1 =?new?File(filePath);//添加了自動(dòng)創(chuàng)建目錄的功能

????((File)file1).mkdir();

????newfileName = System.currentTimeMillis()

????????+ file.getFileName().substring(

????????file.getFileName().lastIndexOf('.'));

????ByteArrayOutputStream baos =?new?ByteArrayOutputStream();

????OutputStream bos =?new?FileOutputStream(filePath +?"/"

????????+ newfileName);

????newpathname = filePath +?"/"?+ newfileName;

????System.out.println(newpathname);

????//?建立一個(gè)上傳文件的輸出流

????System.out.println(filePath +?"/"?+ file.getFileName());

????int?bytesRead = 0;

????byte[] buffer =?new?byte[8192];

????while?((bytesRead = stream.read(buffer, 0, 8192)) != -1)

????{

????????bos.write(buffer, 0, bytesRead);//?將文件寫入服務(wù)器

????}

????bos.close();

????stream.close();

}

catch?(FileNotFoundException e)

{

????e.printStackTrace();

}

catch?(IOException e)

{

????e.printStackTrace();

}

?第二點(diǎn):Jsp頁面上實(shí)現(xiàn)文件上傳

package?com.vogoal.util;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.Hashtable;

import javax.servlet.ServletInputStream;

import javax.servlet.http.HttpServletRequest;

?

public?class?JspFileUpload

{

????/**?request對象?*/

????private?HttpServletRequest request =?null;

????/**?上傳文件的路徑?*/

????private?String uploadPath =?null;

????/**?每次讀取得字節(jié)的大小?*/

????private?static?int BUFSIZE = 1024 * 8;

????/**?存儲(chǔ)參數(shù)的Hashtable?*/

????private?Hashtable paramHt =?new?Hasptable();

????/**?存儲(chǔ)上傳的文件的文件名的ArrayList?*/

????private?ArrayList updFileArr =?new?ArrayList();

????/**

?????*?設(shè)定request對象。

?????*

?????*?@param?request

?????*????????????HttpServletRequest?request對象

?????*/

????public?void setRequest(HttpServletRequest request) {

????????this.request = request;

????}

????/**

?????*?設(shè)定文件上傳路徑。

?????*

?????*?@param?path

?????*????????????用戶指定的文件的上傳路徑。

?????*/

????public?void setUploadPath(String path) {

????????this.uploadPath = path;

????}

文件上傳上處理程序

????/**

?????*?文件上傳處理主程序。???????B

?????*

?????*?@return?int?操作結(jié)果?0?文件操作成功;1?request對象不存在。?2?沒有設(shè)定文件保存路徑或者文件保存路徑不正確;3

?????*?????????沒有設(shè)定正確的enctype;4?文件操作異常。

?????*/

????public?int process() {

????????int status = 0;

????????//?文件上傳前,對request對象,上傳路徑以及enctype進(jìn)行check。

????????status = preCheck();

????????//?出錯(cuò)的時(shí)候返回錯(cuò)誤代碼。

????????if?(status != 0)

????????????return?status;

????????try?{

????????????//???參數(shù)或者文件名?u??

????????????String name =?null;

????????????//?參數(shù)的value

????????????String value =?null;

????????????//?讀取的流是否為文件的標(biāo)志位

????????????boolean fileFlag =?false;

????????????//?要存儲(chǔ)的文件。

????????????File tmpFile =?null;

????????????//?上傳的文件的名字

????????????String fName =?null;

????????????FileOutputStream baos =?null;

????????????BufferedOutputStream bos =?null;

????????????//???存儲(chǔ)參數(shù)的Hashtable

????????????paramHt =?new?Hashtable();

????????????updFileArr =?new?ArrayList();

????????????int rtnPos = 0;

????????????byte[] buffs =?new?byte[BUFSIZE * 8];

????????????//??取得ContentType

????????????String contentType = request.getContentType();

????????????int index = contentType.indexOf("boundary=");

????????????String boundary =?"--"?+ contentType.substring(index + 9);

????????????String endBoundary = boundary +?"--";

????????????//??從request對象中取得流。

????????????ServletInputStream sis = request.getInputStream();

????????????//?讀取1行

????????????while?((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) {

????????????????String strBuff =?new?String(buffs, 0, rtnPos);

????????????????//?讀取1行數(shù)據(jù)?n??

????????????????if?(strBuff.startsWith(boundary)) {

????????????????????if?(name !=?null?&& name.trim().length() > 0) {

????????????????????????if?(fileFlag) {

????????????????????????????bos.flush();

????????????????????????????baos.close();

????????????????????????????bos.close();

????????????????????????????baos =?null;

????????????????????????????bos =?null;

????????????????????????????updFileArr.add(fName);

????????????????????????}?else?{

????????????????????????????Object obj = paramHt.get(name);

????????????????????????????ArrayList al =?new?ArrayList();

????????????????????????????if?(obj !=?null) {

????????????????????????????????al = (ArrayList) obj;

????????????????????????????}

????????????????????????????al.add(value);

????????????????????????????System.out.println(value);

????????????????????????????paramHt.put(name, al);

????????????????????????}

????????????????????}

????????????????????name =?new?String();

????????????????????value =?new?String();

????????????????????fileFlag =?false;

????????????????????fName =?new?String();

????????????????????rtnPos = sis.readLine(buffs, 0, buffs.length);

????????????????????if?(rtnPos != -1) {

????????????????????????strBuff =?new?String(buffs, 0, rtnPos);

????????????????????????if?(strBuff.toLowerCase().startsWith(

????????????????????????????????"content-disposition: form-data; ")) {

????????????????????????????int nIndex = strBuff.toLowerCase().indexOf(

????????????????????????????????????"name="");

????????????????????????????int nLastIndex = strBuff.toLowerCase().indexOf(

????????????????????????????????????""", nIndex + 6);

????????????????????????????name = strBuff.substring(nIndex + 6, nLastIndex);

????????????????????????}

????????????????????????int fIndex = strBuff.toLowerCase().indexOf(

????????????????????????????????"filename="");

????????????????????????if?(fIndex != -1) {

????????????????????????????fileFlag =?true;

????????????????????????????int fLastIndex = strBuff.toLowerCase().indexOf(

????????????????????????????????????""", fIndex + 10);

????????????????????????????fName = strBuff.substring(fIndex + 10, fLastIndex);

????????????????????????????fName = getFileName(fName);

????????????????????????????if?(fName ==?null?|| fName.trim().length() == 0) {

????????????????????????????????fileFlag =?false;

????????????????????????????????sis.readLine(buffs, 0, buffs.length);

????????????????????????????????sis.readLine(buffs, 0, buffs.length);

????????????????????????????????sis.readLine(buffs, 0, buffs.length);

????????????????????????????????continue;

????????????????????????????}else{

????????????????????????????????fName = getFileNameByTime(fName);

????????????????????????????????sis.readLine(buffs, 0, buffs.length);

????????????????????????????????sis.readLine(buffs, 0, buffs.length);

????????????????????????????}

????????????????????????}

????????????????????}

????????????????}?else?if?(strBuff.startsWith(endBoundary)) {

????????????????????if?(name !=?null?&& name.trim().length() > 0) {

????????????????????????if?(fileFlag) {

????????????????????????????bos.flush();

????????????????????????????baos.close();

????????????????????????????bos.close();

????????????????????????????baos =?null;

????????????????????????????bos =?null;

????????????????????????????updFileArr.add(fName);

????????????????????????}?else?{

????????????????????????????Object obj = paramHt.get(name);

????????????????????????????ArrayList al =?new?ArrayList();

????????????????????????????if?(obj !=?null) {

????????????????????????????????al = (ArrayList) obj;

????????????????????????????}

????????????????????????????al.add(value);

????????????????????????????paramHt.put(name, al);

????????????????????????}

????????????????????}

????????????????}?else?{

????????????????????if?(fileFlag) {

????????????????????????if?(baos ==?null?&& bos ==?null) {

????????????????????????????tmpFile =?new?File(uploadPath + fName);

????????????????????????????baos =?new?FileOutputStream(tmpFile);

????????????????????????????bos =?new?BufferedOutputStream(baos);

????????????????????????}

????????????????????????bos.write(buffs, 0, rtnPos);

????????????????????????baos.flush();

????????????????????}?else?{

????????????????????????System.out.println("test :"?+ value +?"--"?+ strBuff);

????????????????????????value = value + strBuff;

????????????????????}

????????????????}

????????????}

????????}?catch?(IOException e) {

????????????status = 4;

????????}

????????return?status;

????}

????private?int preCheck() {

????????int errCode = 0;

????????if?( request ==?null?)

????????????return?1;

????????if?( uploadPath ==?null?|| uploadPath.trim().length() == 0 )

????????????return?2;

????????else{

????????????File tmpF =?new?File(uploadPath);

????????????if?(!tmpF.exists())

????????????????return?2;

????????}

????????String contentType = request.getContentType();

????????if?( contentType.indexOf("multipart/form-data") == -1 )

????????????return?3;

????????return?errCode;

????}

????public?String getParameter(String name){

????????String value =?"";

????????if?( name ==?null?|| name.trim().length() == 0 )

????????????return?value;

????????value = (paramHt.get(name) ==?null)?"":(String)((ArrayList)paramHt.get(name)).get(0);

????????return?value;

????}

????public?String[] getParameters(String name){

????????if?( name ==?null?|| name.trim().length() == 0 )

????????????return?null;

????????if?( paramHt.get(name) ==?null?)

????????????return?null;

????????ArrayList al = (ArrayList)paramHt.get(name);

????????String[] strArr =?new?String[al.size()];

????????for?( int i=0;i<al.size();i++ )

????????????strArr[i] = (String)al.get(i);

????????return?strArr;

????}

???

????public?int getUpdFileSize(){

????????return?updFileArr.size();

????}

???

????public?String[] getUpdFileNames(){

????????String[] strArr =?new?String[updFileArr.size()];

????????for?( int i=0;i<updFileArr.size();i++ )

????????????strArr[i] = (String)updFileArr.get(i);

????????return?strArr;

????}

????private?String getFileName(String input){

????????int fIndex = input.lastIndexOf("\");

????????if?(fIndex == -1) {

????????????fIndex = input.lastIndexOf("/");

????????????if?(fIndex == -1) {

????????????????return?input;

????????????}

????????}

????????input = input.substring(fIndex + 1);

????????return?input;

????}

????private?String getFileNameByTime(String input){

????????int index = input.indexOf(".");

????????Date dt =?new?Date();

????????SimpleDateFormat sdf =?new?SimpleDateFormat("yyyyMMddHHmmssSSS");

????????return?input.substring(0,index) + sdf.format(dt) + input.substring(index);

????}

}

?

?

?2.在Jsp頁面中進(jìn)行引用該Java類:

<%@page?import="com.vogoal.util.JspFileUpload"%>

<%

????//初始化

????JspFileUpload jfu =?new?JspFileUpload();

????//設(shè)定request對象

????jfu.setRequest(request);

????//設(shè)定上傳的文件路徑

????jfu.setUploadPath("C:\");

????//上傳處理

????int?rtn = jfu.process();

????//取得form中其他input控件參數(shù)的值

????String username = jfu.getParameter("username");

????//如果對應(yīng)同一個(gè)參數(shù)有多個(gè)input控件,返回?cái)?shù)組

????String[] usernameArr = jfu.getParameters("username");

????//取得上傳的文件的名字

????String[] fileArr = jfu.getUpdFileNames();

????//取得上傳文件的個(gè)數(shù),這個(gè)方法有點(diǎn)雞肋

????int?fileNumber = jfu.getUpdFileSize();

//下面的是測試輸出的代碼。

//???????out.println("parameter:" + username);

//???????out.println("parameter size:" + usernameArr.length);

//???????out.println("fileArr size:" + fileArr.length);

//???????if (fileArr.length > 0)

//??????????????out.println("fileArr 0:" + fileArr[0]);

%>

?

第三點(diǎn):struts2實(shí)現(xiàn)文件的上傳和下載

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。這兩個(gè)文件可以從Apache Commons – Apache Commons下載。

第二步:把form表的enctype設(shè)置為:“multipart/form-data“,如下:

Java代碼

public?class?UploadAction{?

??private?File uploadImage;?//文件?

??private?String uploadImageContentType;//文件的類型?

??private?String uploadImageFileName;//文件的名稱?

??private?String bookname;//書名?

??private?String author;//作者?

??private?String savePath;//文件的保存位置?

??//屬性的getter/setter方法?

??public?String upload()?throws?Exception{?

??//實(shí)現(xiàn)上傳代碼,I/O操作完成?

??return?"uploadSuccess";?

??}?

}

?

注:一個(gè)表單里的文件域?qū)?yīng)Action中三個(gè)屬性,分別是文件,文件名,文件類型,命名是固定的,文件名必須表單中的文件域名稱相同(uploadImage),文件名為:文件+FileName,文件類型:文件+ContentType。

?

第四步:將我們的上傳Action配置到struts.xml中。

<action?name="upload"?class="com.gqy.UploadAction">?

??????<param?name="savePath">/uploadFile</param>?

??????<result>/success.jsp</result>??</action>

注:指定上傳文件的在服務(wù)器上的保存目錄,需要在UploadAction中為定義savePath變量并為其添加相應(yīng)的setter和getter方法,便于Struts2將/uploadFile值賦給savePath屬性,即要想在UploadAction中使用savePath變量必須在UploadAction定義。

配置文件過濾類型:

<param?name="allowTypes">???????image/bmp,image/png,image/gif,image/jpeg???</param>

手動(dòng)配置文件大小限制

<param?name="maximumSize"?>1048576</param>?

使用Struts2的文件上傳攔截器實(shí)現(xiàn)文件過濾

Struts2提供了一個(gè)文件上傳的攔截器—fileUpload,通過配置該攔截器可以方便實(shí)現(xiàn)上傳文件的過濾。

配置fileUpload攔截器時(shí),可以為其指定兩個(gè)參數(shù):

§ allowedTypes:指定允許上傳的文件類型,多個(gè)文件類型之間以英文逗號(hào)(,)隔開。

§ maximumSize:指定允許上傳的文件大小,單位是字節(jié)。

提示:通過配置fileUpload攔截器,可以輕松的實(shí)現(xiàn)文過濾,當(dāng)文件過濾失敗后,系統(tǒng)自動(dòng)轉(zhuǎn)入input邏輯視圖,因此必須為該Action配置名為input的邏輯視圖,除此之外,還必須顯示地為該Action配置defaultStack的攔截器引用。

使用Struts2的攔截器實(shí)現(xiàn)文件過濾配置如下:

?<action?name="uploadFileAction"?class="com.actions.UploadFileAction">?

???????????<interceptor-ref?name="defaultStack">?

??????????????<!--?配置允許上傳的文件類型,多個(gè)用","分隔?-->??????????????????

??????????????<param?name="fileUpload.allowedTypes">??????????????????????

????????????????????image/bmp,image/png,image/gif,image/jpeg,image/jpg??

????????????????????,image/x-png, image/pjpeg?

??????????????</param>?????????????????

??????????????<!--?配置允許上傳的文件大小,單位字節(jié),本例為:1MB?-->?

??????????????<param?name="fileUpload.maximumSize">1048576</param>?

??????????</interceptor-ref>?

??????????<result?name="input">/jsp/oneFileFileupload.jsp</result>?

??????????<result?name="success">/jsp/result.jsp</result>?

</action>

當(dāng)用戶上傳失敗后,需要有一定的提示信息。在Struts2中,使用<s:fielderror/>標(biāo)簽即可將錯(cuò)誤提示信息輸出到頁面中。

注:要想使用Struts2錯(cuò)誤提示信息,則上傳文件的Action類,必須繼承ActionSupport,否則Struts2不會(huì)提供輸出錯(cuò)誤提示信息功能。

我們可以配置資源文件(.properties)來保存輸出給用戶的信息。

struts.messages.eror.file.too.large:當(dāng)上傳文件大小超過設(shè)定的值時(shí),Struts2將輸出該key對應(yīng)的提示信息。

struts.messages.error.content.type.not.allowed:當(dāng)上傳文件類型不符合設(shè)定的值時(shí),Struts2將輸出該key對應(yīng)的提示信息。

struts.messages.error.uploading:當(dāng)上傳文件時(shí)出現(xiàn)未知錯(cuò)誤時(shí),Struts2將輸出該key對應(yīng)的提示信息。

我們還要將資源文件配置到struts.xml文件中,接下來看看我們的資源文件,已經(jīng)包含中文了,得把它進(jìn)行一下轉(zhuǎn)換再配置到工程中。

在struts.xml中設(shè)定資源文件:

<constant name="struts.custom.i18n.resources" value="messages"/>或

<constant name="struts.custom.i18n.resources" value="messages_zh_CN"/>

用命令native2ascii??d:messages.properties d:messages_zh_CN.properties將原有的資源文件轉(zhuǎn)換成支持中的。

注:保持國際化,資源文件的名稱后綴為: *_zh_CN+文件擴(kuò)展名的形式。

對于多個(gè)文件上傳的原理同上,但是需要注意的是,多個(gè)文件域的name屬性名必須相同,而且在Action中應(yīng)該使用File [] 或者List<File>來接收。

個(gè)人覺得用這樣的方式進(jìn)行多個(gè)文件上傳不是很好。

Struts2進(jìn)行文件下載:

Struts2提供了stream結(jié)果類型,該結(jié)果類型專門用于支持文件下載的功能。當(dāng)指定stream結(jié)果類型時(shí),需要配置一個(gè)inputName參數(shù),該參數(shù)指定了一個(gè)輸入流,這個(gè)輸入流是被下載文件的入口(即通過該入口才能實(shí)現(xiàn)文件以流的方式實(shí)現(xiàn)下載)。

實(shí)現(xiàn)文件下載的Action

?????public?class?FileDownloadAction?implements?Action{?

??????????

????????????//該屬性值在配置文件中指定,Struts2會(huì)自動(dòng)進(jìn)行注入(即賦值),需要為該屬性提供setter和 getter方法?

????????????private?String?inputPath;//指定要下載的文件的完整路徑(路徑名+文件名)?

????????????/*

??????????????*?實(shí)現(xiàn)下載的Action類應(yīng)該提供一個(gè)返回InputStream實(shí)例的方法,該方法對應(yīng)在??????

?????????????????<result.../>里的inputName屬性值為targetFile

????????????*/

????????????public?InputStream?getTargetFile()?throws?Exception{?

???????????????return??ServletActionContext.getServletContext().getResourceAsStream(inputPath);?

????????????}?

????????????//處理用戶請求的execute方法,該方法返回success字符串?

????????????public?String?execute()?throws?Exception{?

???????????????return?"success";?

????????????}

?????????@Override

?????????public?void?doIt(FSM arg0, Input arg1) {

??????????????//?TODO?Auto-generated method stub

?????????????

?????????}?

?????????}

對應(yīng)Action在struts.xml文件中的配置

<action?name="download"?class="com.FileDownloadAction">

???<!--指定被下載資源的位置-->

??????<param?name="inputPath">/uploadFile/demo.txt</param>

???<!--配置結(jié)果類型為stream的結(jié)果-->

???<result?name="success"?type="stream">

???????<!--指定下載文件的文件類型-->

??????????<param?name="contentType"></param>

???????<!--指定下載文件的文件位置-->

??????????<param?name="inputName">targetFile</param>

???????<!--指定下載文件的下載方式及下載時(shí)的保存文件名,filename保存時(shí)的文件名必須有擴(kuò)展名,擴(kuò)展名指示了下載類型的圖標(biāo)-->

??????????<param?name="contentDisposition">

????????????????attachment;filename=Struts2.txt

??????????</param>

???????<!--指定下載文件的緩沖區(qū)大小-->

??????????<param?name="bufferSize">4096</param>

???</result>

</action>

后端代碼邏輯大部分是相同的,目前能夠支持MySQL,Oracle,SQL。在使用前需要配置一下數(shù)據(jù)庫,可以參考我寫的這篇文章:java http大文件斷點(diǎn)續(xù)傳上傳 – 澤優(yōu)軟件博客

歡迎入群一起討論“374992201”

?

本文摘自 :https://www.cnblogs.com/

開通會(huì)員,享受整站包年服務(wù)立即開通 >