欢迎光临
我们一直在努力

一个完整的上传bean—转载-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

package popeyelin;
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:/";//目标文件目录
public string[] suffix = new string[255];//文件后缀名
public string[] objectfilename = new string[255];//目标文件名
public servletinputstream sis = null;//输入流
public string[] description = new string[255];//描述状态
public long size = 100*1024;//限制大小
private int count = 0;//已传输文件数目
private byte[] b = new byte[4096];//字节流存放数组
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();
//这个是我用来传图片的,各位可以把后缀名改掉或者不要这个条件
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;//标示已经传输的字节数
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(){
//可以在构建器里面构建服务器上传目录,也可以在javabean调用的时候自己构建
setobjectpath("/home/www/jspvhost4/web/popeyelin/images/");
}
}  

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个完整的上传bean—转载-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章