通过DataInputStream和DataOutputStream来编写一个上载类即可。
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{ }
在客户端的form中使用enctype="multipart/form-data"
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{ }
在客户端的form中使用enctype="multipart/form-data"
在客户端的form中使用enctype= "multipart/form-data " 我也不明白这个,你能给我一个客户端应用的简单例子吗?如果不方便贴出,可以发到我的邮箱里[email protected]
定有重谢!
搜索本论坛也可以找的到!
客户端页面内容如下:
<form name="loadform" method="post" enctype="multipart/form-data" action="handler.jsp">
<table border="0" align="center" cellpadding="0" cellspacing="0" width="300">
<tr>
<td>选择文件:</td>
</tr>
<tr>
<td><input type="file" name="FILENAME" size="30"></td>
</tr>
<tr>
<td align="center">
<p> </p>
<a href="javascript:loadform.submit();">上载</a>
<a href="javascript:window.close();">关闭</a>
</td>
</tr>
</table>
</form>
然后在handler.jsp页面中实例化上载类并调用上载方法(传递参数为request)即可。