今天新写了一个java文件上传类,文件类型未限定,大小也未限定,可自己扩展,记下笔记
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
* Function: FileUpload
* Writer: JingJiaGuo
* Date: 09.5.12
* Mail: guojingjia@stu.hdu.edu.cn
*/
package JingJiaGuo;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class FileUpload
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class FileHoder
{
public String name,filename,contentType;
public int start,end,length;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class ValueHoder
{
public String name,value;
}
public ArrayList<FileHoder> arrfile;
public ArrayList<ValueHoder> arrvalue;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String FileNameSuffix(int index)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(index<arrfile.size())
{
int pos=arrfile.get(index).filename.lastIndexOf(".");
return arrfile.get(index).filename.substring(pos);
}else return null;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getFileName(int index)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(index<arrfile.size())
{
return arrfile.get(index).filename;
}else return null;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int getFileSize(int index)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(index<arrfile.size())
{
return arrfile.get(index).length;
}else return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getParamentName(int index)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(index<arrvalue.size())
{
return arrvalue.get(index).name;
}else return null;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getParamentValue(int index)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(index<arrvalue.size())
{
return arrvalue.get(index).value;
}else return null;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void Clear()
{
arrfile.clear();
arrvalue.clear();
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public boolean Parse(HttpServletRequest request)
{
arrfile=new ArrayList<FileHoder>();
arrvalue=new ArrayList<ValueHoder>();
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
String contenttype=request.getContentType();
String boundary="boundary=";
int pos=contenttype.indexOf(boundary);
boundary=contenttype.substring(pos+boundary.length());
boundary="--"+boundary;
InputStream is=request.getInputStream();
int readlen=0,len=request.getContentLength();
buffer=new byte[len];
int read;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while((read=is.read(buffer,readlen,len-readlen))>0)
{
readlen+=read;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(readlen<len)
{
return false;
}
int st=byteIndexOf(buffer,boundary,0,len);
int ed=0;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(st>=0)
{
st+=boundary.length();// /r/n
ed=byteIndexOf(buffer,boundary,st,len);
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(ed!=-1)
{
Process(buffer,st+2,ed-2);
}
else break;
st=ed;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}catch(IOException e)
{
e.printStackTrace();
return false;
}
return true;
}
private void Process(byte[] buffer,int st,int ed)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws UnsupportedEncodingException
{
int[] pos=new int[tokens.length];
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=0;i<tokens.length;++i)
{
pos[i]=byteIndexOf(buffer,tokens[i],st,ed);
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(pos[1]==-1)
{
ValueHoder vh=new ValueHoder();
vh.name=subBytesString(buffer,pos[0]+ByteLength(tokens[0]),pos[2]);
vh.value=subBytesString(buffer,pos[4]+ByteLength(tokens[4]),ed);
arrvalue.add(vh);
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
FileHoder fh=new FileHoder();
fh.name=subBytesString(buffer,pos[0]+ByteLength(tokens[0]),pos[1]);
fh.filename=subBytesString(buffer,pos[1]+ByteLength(tokens[1]),pos[2]);
fh.contentType=subBytesString(buffer,pos[3]+ByteLength(tokens[3]),pos[4]);
fh.start=pos[4]+ByteLength(tokens[4]);
fh.end=ed;
fh.length=fh.end-fh.start;
// fh.filecontent=subBytesString(buffer,pos[4]+ByteLength(tokens[4]),ed);
if(fh.length>0)arrfile.add(fh);
}
}
public void SaveTo(int index,String path)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws IOException,FileNotFoundException
{
FileOutputStream fos=new FileOutputStream(path);
fos.write(buffer,arrfile.get(index).start,arrfile.get(index).length);
fos.close();
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private int ByteLength(String str) throws UnsupportedEncodingException
{
byte[] tmp=str.getBytes();
return tmp.length;
}
private String subBytesString(byte[] buffer,int from,int end,String encoding)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws UnsupportedEncodingException
{
byte[] tmp=new byte[end-from];
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=from;i<end;i++)
{
tmp[i-from]=buffer[i];
}
return new String(tmp,encoding);
}
private String subBytesString(byte[] buffer,int from,int end)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws UnsupportedEncodingException
{
return subBytesString(buffer,from,end,"UTF-8");
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private int byteIndexOf(byte[] buffer,String src,int begin,int end)
{
byte[] boundary=src.getBytes();
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=begin;i<end;i++)
{
boolean find=true;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int j=0;j<src.length()&&j+i<end;j++)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(buffer[i+j]!=boundary[j])
{
find=false;
break;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(find)
{
return i;
}
}
return -1;
}
private byte[] buffer;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private String[] tokens=
{"name=\"",
"\"; filename=\"",
"\"\r\n",
"Content-Type: ",
"\r\n\r\n"
};
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
example:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.io.*" %>
<%@page import="JingJiaGuo.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
FileUpload myfile=new FileUpload();
myfile.Parse(request);
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
for(int i=0;i<myfile.arrfile.size();i++)
{
myfile.SaveTo(i,"D:\\"+myfile.getFileName(i));
out.write(myfile.getFileName(i)+"<br>");
out.write(myfile.arrfile.get(i).contentType+"<br>");
out.write(myfile.FileNameSuffix(i));
//out.write(myfile.arrfile.get(i).filecontent.length()+"<br>");
}
myfile.Clear();
%>
</body>
</html>
posted on 2009-05-12 22:57
小果子 阅读(229)
评论(0) 编辑 收藏 引用 所属分类:
学习笔记