谁有实现文件上传得的源代码,用javabean做的,一定给分

xiaoduzi 2002-05-11 05:18:24
我要源代码,多谢,email:smallduzi@163.com
...全文
103 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
秋风ss落叶 2002-05-11
www.chinajavaworld.com有下载的!
回复
saintKnight 2002-05-11
import java.io.*;
import java.util.*;

public class fileLoad {
String filepath="c:\\upload";
public fileLoad() {
}
public void uploadFile(javax.servlet.http.HttpServletRequest req) throws IOException,Exception {
try {
String contentType=req.getContentType();
int contentLength=req.getContentLength();
java.io.DataInputStream bis=new java.io.DataInputStream(req.getInputStream());
int once = 0;
int total = 0;
byte[] buffer=new byte[contentLength];
while ((total<contentLength) && (once>=0)) {
once = bis.read(buffer,total,contentLength);
total += once;
}
int boundaryStart=contentType.indexOf("boundary=");
boundaryStart=boundaryStart+"boundary=".length();
String boundary="--"+contentType.substring(boundaryStart);
int pos=getFormNameIndex(buffer,"filename=\"".getBytes(),1)+"filename=\"".length();
int posEnd=getFormNameIndex(buffer,"\"".getBytes(),pos);
String filename=new String(buffer,pos,posEnd-pos);
pos=filename.lastIndexOf(".");
String filenames=System.currentTimeMillis()/1000+filename.substring(pos);
pos=getFormNameIndex(buffer,"Content-Type: ".getBytes(),1);
pos=getFormNameIndex(buffer,"\r\n".getBytes(),pos+1)+4;
int endpos=getFormNameIndex(buffer,boundary.getBytes(),pos+20);
int len=endpos-pos;
java.io.DataOutputStream bos= new java.io.DataOutputStream(new java.io.FileOutputStream(new java.io.File(filepath,filenames)));
bos.write(buffer,pos,len);
bis.close();
bos.close();
}catch(Exception exc) {
throw new Exception("upload failed.");
}finally{

}
}
private int getFormNameIndex(byte[] source,byte[] formname,int start) {
int soulen=source.length;
int sealen=formname.length;
boolean hasSearch=false;
int pos=-1;
for(int i=start;i<soulen;i++) {
if(source[i]==formname[0]) {
boolean hasSear=true;
for(int k=1;k<sealen;k++) {
if(source[i+k]!=formname[k]) {
hasSear=false;
break;
}
}
hasSearch=hasSear;
}
if(hasSearch) {
pos=i;
break;
}
}
return pos;
}
}
回复
weidegong 2002-05-11
http://www-900.ibm.com/developerWorks/java/fileup/index.shtml

http://www.code-labs.com/article/articleinfo.php?id=542

smartUpload不行吗?
回复
hutlyx 2002-05-11
网上很多的,去代码下载的网站,一般都有
回复
alphazhao 2002-05-11
自个搜索论坛吧
应该有不少关于这方面的问题
看看这个例子http://www.csdn.net/expert/topic/651/651822.xml?temp=.2982447
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告