回 帖 发 新 帖 刷新版面

主题:上传文件容量大小以及路径问题

下面是用java servlet写的关于文件上传的代码
我测试了一下,发觉,可以上传文件大小在12M左右,上传到的地方应该是默认的地方吧(D:\tomcat\bin\).
我想用它上传30M左右的文件,路径也想改变一下,比如D:\或E:\都行,我不知道怎么修改?
请帮帮忙,谢谢了!!
UploadFileServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class UploadFileServlet extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
final int NONE=0;
final int DATAHEADER=1;
final int FILEDATA=2;
final int FIELDDATA=3;

int totalbytes=req.getContentLength();
byte[] b=new byte[totalbytes];

String contentType=req.getContentType();
String fieldname="";
String fieldvalue="";
String filename="";
String boundary="";
String lastboundary="";
int fileSize=0;

Hashtable formfields=new Hashtable();

int pos=contentType.indexOf("boundary=");
String fileID;
if(pos!=-1){
pos+="boundary=".length();
boundary="--"+contentType.substring(pos);
lastboundary=boundary+"--";
}
int state=NONE;

回复列表 (共2个回复)

沙发

接着上面的

DataInputStream in=new DataInputStream(req.getInputStream());
in.readFully(b);
in.close();
String reqcontent=new String(b);

BufferedReader reqbuf=new BufferedReader(new StringReader(reqcontent));

boolean flag=true;
int i=0;
while(flag==true){
String s=reqbuf.readLine();
if(s==lastboundary||s==null)break;
switch(state){
case NONE:
if(s.startsWith(boundary)){

state=DATAHEADER;
i+=1;
}
break;
case DATAHEADER:
pos=s.indexOf("filename=");

if(pos==-1){
pos=s.indexOf("name=");
pos+="name=".length()+1;
s=s.substring(pos);
int l=s.length();
s=s.substring(0,l-1);
fieldname=s;
state=FIELDDATA;
}
else{

String temp=s;

pos=s.indexOf("filename=");
pos+="filename=".length()+1;
s=s.substring(pos);
int l=s.length();
s=s.substring(0,l-1);
pos=s.lastIndexOf("\\");
s=s.substring(pos+1);
filename=s;

pos=byteIndexOf(b,temp,0);
b=subBytes(b,pos+temp.getBytes().length+2,b.length);

s=reqbuf.readLine();
                           
File f=new File(filename);
DataOutputStream fileout=new DataOutputStream(new FileOutputStream(f));
b=subBytes(b,s.getBytes().length+4,b.length);
pos=byteIndexOf(b,boundary,0);
b=subBytes(b,0,pos-1);
fileout.write(b,0,b.length-1);
fileSize=b.length-1;
state=FILEDATA;
}
break;
case FIELDDATA:
s=reqbuf.readLine();
fieldvalue=s;
formfields.put(fieldname,fieldvalue);
state=NONE;
break;
case FILEDATA:

while((!s.startsWith(boundary))&&(!s.startsWith(lastboundary))){
s=reqbuf.readLine();
if(s.startsWith(boundary)){
state=DATAHEADER;
}
else break;
break;
}

}
}

板凳

接着上面的

res.setContentType("text/html;charset=gb2312");
PrintWriter out=res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>文件上传结果</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>文件上传结果</H1><hr>");
out.println("ID为"+formfields.get("FileID")+"的文件"+filename+"已经上传!"+
                                 "文件长度为:"+fileSize+"字节");
out.println("</BODY>");
out.println("</HTML>");
}

private static int byteIndexOf(byte[] b,String s,int start)
{
return byteIndexOf(b,s.getBytes(),start);
}


private static int byteIndexOf(byte[] b,byte[] s,int start)
{
int i;
if(s.length==0)
{
return 0;
}
int max=b.length-s.length;
if(max<0)
return -1;
if(start>max)
return -1;
if(start<0)
start=0;

search:
for(i=start;i<=max;i++)
{
if(b[i]==s[0])
{
int k=1;
while(k<s.length)
{
if(b[k+i]!=s[k])
{
continue search;
}
k++;
}
return i;
}
}
return -1;
}

private static byte[] subBytes(byte[] b,int from,int end)
{
byte[] result=new byte[end-from];
System.arraycopy(b,from,result,0,end-from);
return result;
}

private static String subBytesString(byte[] b,int from,int end)
{
return new String(subBytes(b,from,end));
}
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册