一个完整的JSP上传上传JavaBean


关键词

一个完整的JSP上传上传JavaBean

摘要

//Title: Cnjsp"s Project 
//Version:
//Copyright: Copyright (c) 1999
//Author: Popeye
//Company: Cnjsp
//Description: It is for cnjsp

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;


//Title: Cnjsp"s Project
//Version:
//Copyright: Copyright (c) 1999
//Author: Popeye
//Company: Cnjsp
//Description: It is for cnjsp

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/");
}
}


 

要饭二维码

洪哥写文章很苦逼,如果本文对您略有帮助,可以扫描下方二维码支持洪哥!金额随意,先行谢过!大家的支持是我前进的动力!

文章的版权

本文属于“洪哥笔记”原创文章,转载请注明来源地址:一个完整的JSP上传上传JavaBean:http://www.splaybow.com/post/jspallupload.html

如果您在服务器运维、网络管理、网站或系统开发过程有需要提供收费服务,请加QQ:8771947!十年运维经验,帮您省钱、让您放心!
亲,如果有需要,先存起来,方便以后再看啊!加入收藏夹的话,按Ctrl+D

« JavaBean实现多文件上传的两种方法 用Java发送传真解决之道 »

相关文章:

JSP动态包含  (2014/6/28 8:32:24)

JSP静态包含  (2014/6/27 8:24:43)

Java参数化查询无法得到可更新的记录集  (2014/6/26 9:29:00)

jsp包含文件与被包含文件的contentType必须完全一致  (2014/6/25 9:19:33)

Java可以定义元素个数为0的数组  (2014/6/23 11:24:23)

Java中XML的构造与输出  (2014/3/29 11:51:08)

Java中如何把整形int转换成字符串String类型  (2014/3/28 11:31:28)

自动添加超链接  (2014/1/17 9:43:23)

使用keytool生成私钥  (2014/1/2 20:58:48)

Java递归算法示例  (2013/12/29 19:19:50)