java jsp 文件上传_JSP实现快速上传文件的方法

本文实例讲述了JSP实现快速上传文件的方法。分享给大家供大家参考。具体如下:

这里演示JSP不使用第三方库,实现快速上传文件的功能

1. FileUpload.java:

package FileUpload;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import javax.servlet.ServletInputStream;

/**

*

*/

/**

* @author Qch

*

*/

public class FileUpload

{

ServletInputStream in=null;

String fpath="C://";

public FileUpload()

{

fpath="C://";

in=null;

}

public void setInputStream(ServletInputStream in)

{

this.in=in;

}

public void setFpath(String p)

{

this.fpath=p;

}

public String getFpath()

{

return fpath;

}

public String getParameter()

{

String r=null;

try

{

r=getParameter(in);

}

catch (Exception e)

{

e.printStackTrace();

}

return r;

}

public long getFileUpload()

{

long r=-1;

try

{

r=getFileUpload(in,fpath);

}

catch (Exception e)

{

e.printStackTrace();

}

return r;

}

public String getParameter(ServletInputStream in)// 只能按顺序提取

throws Exception

{

int l = 0;

byte[] b = new byte[1024];

l = in.readLine(b, 0, b.length);// 依次是读取属性的开始符、名称、属性值的类型、属性的值

String si = new String(b);

if (si.startsWith("----------------------------"))

{// 表示是从开始符开始读,否则应为刚读取文件后的一个属性,此时应少读一次

l = in.readLine(b, 0, b.length);

}

l = in.readLine(b, 0, b.length);

l = in.readLine(b, 0, b.length);

String value = new String(b, 0, l);

return value;

}

public long getFileUpload(ServletInputStream in, String fpath)// 需要提供输入流和存储路径

throws Exception

{

// out.println("文件信息:
");

long begin = System.currentTimeMillis();// 传送时间计时开始

int l = 0;

byte[] b = new byte[1024];

l = in.readLine(b, 0, b.length);

String sign = new String(b, 0, l);// eg.-----------------------------7d9dd29630a34

l = in.readLine(b, 0, b.length);

String info = new String(b, 0, l);// eg.Content-Disposition:form-data;

// name="file";

l = in.readLine(b, 0, b.length);

// String type=new

// String(b,0,l);//eg.Content-Type:application/octet-stream(程序文件)

l = in.readLine(b, 0, b.length);

// String nulll=new String(b,0,l);//此值应为空

int nIndex = info.toLowerCase().indexOf("filename=\"");

int nLastIndex = info.toLowerCase().indexOf("\"", nIndex + 10);

String filepath = info.substring(nIndex + 10, nLastIndex);

int na = filepath.lastIndexOf("\\");

String filename = filepath.substring(na + 1);

// out.println("文件绝对路径:"+filepath+"
");

// out.println("文件名:"+filename+"
");

String path=fpath + filename;

File fi = new File(path);// 建立目标文件

if (!fi.exists()&&!fi.createNewFile())

return -2;

BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream(

fi));

while ((l = in.readLine(b, 0, b.length)) > 0)

{

if (l == sign.length())

{

String sign1 = new String(b, 0, sign.length());

// out.println(sign1+"
");

if (sign1.startsWith(sign))// 比对是否文件已传完

break;

}

f.write(b, 0, l);

f.flush();

}

f.flush();

f.close();

long end = System.currentTimeMillis();// 传送时间计时结束

// out.println("上传文件用时:"+(end-begin)+"毫秒
");

return end - begin;

}

}

这就是微学网-程序员之家为你提供的"JSP实现快速上传文件的方法"希望对你有所帮助.本文来自网络,转载请注明出处:http://www.weixuecn.cn/article/4862.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值