该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
对于学习JSP/SERVLET的人来说,学会使用jar包实现文件上传的只是一种入门,就好像别人写了一个类,你知道调用类的main方法一样。但是,我们不仅要知道怎么调用main方法,更要知道main方法中写的是什么。最近对文件上传下载感兴趣,就找了找相关资料,结合自己所学,做了一个文件上传的例子。便于大家的学习。
例子的功能:
实现多文件上传下载
实现获取提交的form表单的其他项
实现获取上传文件的列表
注意:本例子在struts下失效,原因是struts对request进行过封装。
文件只有两个。
1.test.jsp
pageEncoding="utf-8"%>
首页enctype="multipart/form-data">
值1:
值2:
路径1:
路径2:
2.FileUpload.java
importjava.io.BufferedOutputStream;
/**
* 上传附件
*/
@WebServlet("/FileUpload")
publicclass FileUpload extends HttpServlet {
private String fpath="c:/";
private Map map=newHashMap();
private List files=newArrayList();
private longgetFileUpload(HttpServletRequest request) throws IOException{
longstart=System.currentTimeMillis();
ServletInputStream in =request.getInputStream();
//组装上传文件所需要的数据
getFileUpload(in,getSign(request),fpath);
longend=System.currentTimeMillis();
return end-start;
}
//获取文件的分割线标志
public String getSign(HttpServletRequestrequest){
String contentType =request.getContentType();
intindex = contentType.indexOf( "boundary=" );
return "--" + contentType.substring( index+ 9 );
}
//输入流,分割标志,上传位置
private voidgetFileUpload(ServletInputStream in,String sign, String fpath) throwsIOException {
int l=0;
byte[] b=new byte[1024];
//用来指示要上传的文件
File file=null;;
BufferedOutputStreamout=null;
//用来存放form表单中的value的值。这儿考虑到textarea等,所以用到了sb
StringBuilder sb=newStringBuilder();
//用来存放form中的name值
Stringname="";
while((l=in.readLine(b,0, b.length))>0){
if(file==null) {
Stringtemp=new String(b,0,l);
StringlowerTemp=temp.toLowerCase();
if(lowerTemp.startsWith( "content-disposition: form-data; " )) {
if(lowerTemp.contains("filename=\"") &&!lowerTemp.contains("filename=\"\"")) {
intnIndex=lowerTemp.indexOf("filename=\"");
intnLastIndex=temp.indexOf("\"",nIndex+10);
StringfilePath=temp.substring(nIndex+10, nLastIndex);
Stringfilename=filePath.substring(filePath.lastIndexOf("\\")+1);
file=newFile(fpath+filename);
out=newBufferedOutputStream(new FileOutputStream(file));
in.readLine(b, 0,b.length);
}elseif (lowerTemp.contains("name=\"")) {
name=temp.substring(lowerTemp.indexOf("name=\"")+6,temp.lastIndexOf("\""));
}
in.readLine(b,0, b.length);
}elseif (!"".equals(name)){
if(temp.startsWith(sign)) {
map.put(name,sb.toString());
name="";
sb=newStringBuilder();
}else
sb.append(newString(b,0,l-2));
}
continue;
}
if(l==sign.length()+2 || l==sign.length()+4) {
Stringtemp=new String(b,0,l);
if(temp.startsWith(sign)) {
out.flush();
out.close();
files.add(newFile(file.getAbsolutePath()));
file=null;
out=null;
continue;
}
}
out.write(b,0,l);
out.flush();
}
if (file!=null) {
files.add(newFile(file.getAbsolutePath()));
}
}
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
request.setCharacterEncoding("GBK");
longtime=getFileUpload(request);
System.out.println("上传文件花费时间为:"+time+"毫秒,文件上传位置为:"+fpath);
System.out.println("form表单的值为:");
for(String key : map.keySet()) {
System.out.println(key+":"+map.get(key));
}
System.out.println();
System.out.println("上传的文件有:");
for(Filefile:files){
System.out.println(file.getName());
}
response.getWriter().write("success!!!");
}
}