上传问价限制大小java,上传时限制文件类型和文件大小。

当前位置:我的异常网» Java Web开发 » 上传时限制文件类型和文件大小。

上传时限制文件类型和文件大小。

www.myexceptions.net  网友分享于:2013-12-03  浏览:1506次

上传时限制文件类型和文件大小。。。。。。。。

我用servlet来做上传,代码如下:

public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)

throws   ServletException,   IOException   {

response.setContentType( "text/html;charset=gb2312 ");

HttpSession   session   =   request.getSession();

PrintWriter   out   =   response.getWriter();

String   tempfilename   =   (String)   session.getId();   //   会话ID

String   path   =   request.getRealPath( "/ ")   +   "Accessory/ ";   //   取物理路经

File   f1   =   new   File(path,   tempfilename);     //File对象

int   n;

try   {

InputStream   in   =   request.getInputStream();   //   取得请求输入流

BufferedInputStream   my_in   =   new   BufferedInputStream(in);

FileOutputStream   fout   =   new   FileOutputStream(f1);

BufferedOutputStream   my_out   =   new   BufferedOutputStream(fout);

byte[]   b   =   new   byte[10000];

while   ((n   =   my_in.read(b))   !=   -1)   {

my_out.write(b,   0,   n);

}

my_out.flush();

my_out.close();

fout.close();

my_in.close();

in.close();

//读取临时文件f1,从中获取上传文件的名字和上传文件的内容。

RandomAccessFile   random1   =   new   RandomAccessFile(f1,   "r ");

random1.readLine();

String   filename   =   random1.readLine();

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值