用smartupload
http://www.jspsmart.com/
http://www.jspsmart.com/
解决方案 »
- java 开发一个extjs的问题 为什么我的树加载不出来?
- struts2 if标签的中文判断没反应是什么原因?
- 急!急!急!急!无法跳转到servlet页面
- 网络视频技术
- 解析一个嵌套的json,谢谢。
- 请问大家的Hibernate项目的类结构是怎么样的?我们的使用了DAO模式,一个表对应一个dao接口和一个实现dao,但我感觉很多代码都是重复的,
- 紧急求助,有谁使用过水晶报表!!!!!!!!!!!!!??
- jsp中文乱码问题?》
- jsp与SQLserver2000的连接问题(能连接本地数据库,但不能连接远程数据库?)
- 在java或jsp中,如何将web页上表格中的数据导入到excel中?多谢了,一定给分,希望直接给程序,不要写连接,因为我现在上望不方便
- 为什么我的Tomcat突然找不到Serverlet了
- 怎么样可以将session的存活期限设置成“永远不死”??
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;
}
JSP页面:
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
fileUpLoad
</title>
</head>
<body>
<form name=fileload method=post action="fileUpLoadOp.jsp">
文件名:<input type="file" name="filename"><br>
文件夹:<input type="checkbox" name="uploadfolder" value="1">
<input type="submit" name="submit" value="上载">
</form>
</body>
</html>
JSP处理页面:
<jsp:useBean id="fload" scope="page" class="learn.load.fileLoad" />
<%
fload.uploadFile(request);
%>
§http://www-900.ibm.com/developerWorks/java/fileup/index.shtml