用jsp上传,怎样把文件传到服务器?高分另外开贴!

henry_t 2002-06-03 09:31:43
我在做一个用jsp的上传下载,现在从服务器上把文件传到服务器上的数据库oracle大对象实验已经成功,但由于我接触网络不太久,所以,对于怎样把客户端的文件传到服务器上却一头雾水,我想应该不难,好象是依靠http协议是吧?要知道上传是先要把文件从客户端传到服务器,再才是传到数据库啊,所以请大家指教,小弟非常感激!希望能仔细一点,写出代码,献上200!(另外发贴给!)
...全文
52 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangqi 2002-06-03
  • 打赏
  • 举报
回复
上载:
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);
%>
henry_t 2002-06-03
  • 打赏
  • 举报
回复
我想直接写进数据库
henry_t 2002-06-03
  • 打赏
  • 举报
回复
我试过的!!!不过,现在我想自己写一个!!!!!,希望指教!!
tehcon 2002-06-03
  • 打赏
  • 举报
回复
用smartupload
http://www.jspsmart.com/
weidegong 2002-06-03
  • 打赏
  • 举报
回复
那就参考一下这个:
§http://www-900.ibm.com/developerWorks/java/fileup/index.shtml
gallipot 2002-06-03
  • 打赏
  • 举报
回复
不好意思,怎样获取上载的文件名呢??

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧