common-uploadfiles.1.2版本

servlet中测试成功:

java 代码
  1. String filesystempath = request.getSession().getServletContext().getRealPath("/");   
  2. ServletRequestContext srcontext = new ServletRequestContext(request);   
  3. System.out.println(srcontext.getCharacterEncoding());   
  4. System.out.println(srcontext.getContentType());   
  5. boolean isMultipart = ServletFileUpload.isMultipartContent(request);   
  6. if (isMultipart) {   
  7.     ServletFileUpload upload = new ServletFileUpload();   
  8.     try {   
  9. //Parse the request   
  10.     FileItemIterator iter = upload.getItemIterator(request);   
  11.     while (iter.hasNext()) {   
  12.         FileItemStream item = iter.next();   
  13.         //System.out.println(item.getContentType());
  14.         InputStream stream = item.openStream();   
  15.         if (item.isFormField()) {   
  16.                                            //这里处理form中的其它表单域内容   
  17.         } else {   
  18.                                            //这里处理上传对象   
  19.         String clientrealpath = item.getName();   
  20.         clientrealpath = clientrealpath.replace("\\", "\\\\");   
  21. //System.out.println(clientrealpath);   
  22. String filename = StringHelper.getFileName(clientrealpath);   
  23. //System.out.println(filesystempath+filename);   
  24. File newfile = new File(filesystempath+filename);   
  25. OutputStream os =new FileOutputStream(newfile);   
  26. byte[] b = new byte[8192];   
  27. int len;   
  28. while ((len = stream.read(b)) != -1) {   
  29. os.write(b, 0, len);   
  30. os.flush();   
  31. os.close();   
  32. stream.close();   
  33. // Process the input stream   
  34. }   
  35. }   
  36. catch (FileUploadException e) {   
  37. e.printStackTrace();   
  38. }   
  39. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值