java 文件上传 jar_发一个用纯servlet实现的文件上传示例,不需要额外的jar包

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

对于学习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!!!");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值