本篇內(nèi)容主要講解“怎么編寫一個(gè)完整的上傳文件bean”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么編寫一個(gè)完整的上傳文件bean”吧!
創(chuàng)新互聯(lián)是工信部頒發(fā)資質(zhì)IDC服務(wù)器商,為用戶提供優(yōu)質(zhì)的成都聯(lián)通服務(wù)器托管服務(wù)import java.io.*;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
public class transfer_multi {
public String[] sourcefile = new String[255];//源文件名
public String objectpath = "c:/";//目標(biāo)文件目錄
public String[] suffix = new String[255];//文件后綴名
public String[] objectfilename = new String[255];//目標(biāo)文件名
public ServletInputStream sis = null;//輸入流
public String[] description = new String[255];//描述狀態(tài)
public long size = 100*1024;//限制大小
private int count = 0;//已傳輸文件數(shù)目
private byte[] b = new byte[4096];//字節(jié)流存放數(shù)組
private boolean successful = true;
public void setSourcefile(HttpServletRequest request) throws java.io.IOException{
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if((k = s.indexOf("filename=")) != -1){
s = s.substring(k+10);
k = s.indexOf(""");
s = s.substring(0,k);
sourcefile[count] = s;
k = s.lastIndexOf(".");
suffix[count] = s.substring(k+1);
System.out.println(suffix[count]);
if(canTransfer(count)) transferfile(count);
}
if(!successful) break;
}
}
public int getCount(){
return count;
}
public String[] getSourcefile(){
return sourcefile;
}
public void setObjectpath(String objectpath){
this.objectpath = objectpath;
}
public String getObjectpath(){
return objectpath;
}
private boolean canTransfer(int i){
suffix[i] = suffix[i].toLowerCase();
//這個(gè)是我用來傳圖片的,各位可以把后綴名改掉或者不要這個(gè)條件
if(sourcefile[i].equals("")||(!suffix[i].equals("gif")&&!suffix[i].equals("jpg")&&!suffix[i].equals("jpeg"))) {description[i]="ERR suffix is wrong";return false;}
else return true;
}
private void transferfile(int i){
String x = Long.toString(new java.util.Date().getTime());
try{
objectfilename[i] = x+"."+suffix[i];
FileOutputStream out = new FileOutputStream(objectpath+objectfilename[i]);
int a = 0;
int k = 0;
long hastransfered = 0;//標(biāo)示已經(jīng)傳輸?shù)淖止?jié)數(shù)
String s = "";
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if((k = s.indexOf("Content-Type:")) != -1) break;
}
sis.readLine(b,0,b.length);
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if((b[0]==45)&&(b[1]==45)&&(b[2]==45)&&(b[3]==45)&&(b[4]==45)) break;
out.write(b,0,a);
hastransfered+=a;
if(hastransfered>=size){
description[count] = "ERR The file "+sourcefile[count]+" is too large to transfer. The whole process is interrupted.";
successful = false;
break;
}
}
if(successful) description[count] = "Right The file "+sourcefile[count]+" has been transfered successfully.";
++count;
out.close();
if(!successful){
sis.close();
File tmp = new File(objectpath+objectfilename[count-1]);
tmp.delete();
}
}
catch(IOException ioe){
description[i]=ioe.toString();
}
}
public transfer_multi(){
//可以在構(gòu)建器里面構(gòu)建服務(wù)器上傳目錄,也可以在javabean調(diào)用的時(shí)候自己構(gòu)建
setObjectpath("/home/www/jspvhost4/web/popeyelin/images/");
}
}
到此,相信大家對(duì)“怎么編寫一個(gè)完整的上傳文件bean”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文名稱:怎么編寫一個(gè)完整的上傳文件bean-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://m.rwnh.cn/article48/gephp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站營銷、網(wǎng)站改版、動(dòng)態(tài)網(wǎng)站、建站公司、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容