当前位置: 技术问答>java相关
哪位给个jsp上传文件的例子,这里先谢谢了!
来源: 互联网 发布时间:2015-06-24
本文导语: 请尽量详细些 | 很多人用jspsmartUpload: http://www.jspsmart.com/ 有javaBean类文件下载和例子演示 | 上载: public void uploadFile(javax.servlet.http.HttpServletRequest req) throws IOException,Except...
请尽量详细些
|
很多人用jspsmartUpload:
http://www.jspsmart.com/ 有javaBean类文件下载和例子演示
http://www.jspsmart.com/ 有javaBean类文件下载和例子演示
|
上载:
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=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,"rn".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
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=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,"rn".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