上传文件的Bean

java 代码
  1. import java.io.*;   
  2. import javax.servlet.ServletInputStream;   
  3. import javax.servlet.http.HttpServletRequest;   
  4.   
  5. public class transfer_multi {   
  6.        
  7.     public String[] sourcefile = new String[255];// 源文件名   
  8.     public String objectpath = "c:/";// 目标文件目录   
  9.     public String[] suffix = new String[255];// 文件后缀名   
  10.     public String[] objectfilename = new String[255];// 目标文件名   
  11.     public ServletInputStream sis = null;// 输入流   
  12.     public String[] description = new String[255];// 描述状态   
  13.     public long size = 100 * 1024;// 限制大小   
  14.     private int count = 0;// 已传输文件数目   
  15.     private byte[] b = new byte[4096];// 字节流存放数组   
  16.     private boolean successful = true;   
  17.        
  18.     public void setSourcefile(HttpServletRequest request)   
  19.             throws java.io.IOException {   
  20.         sis = request.getInputStream();   
  21.         int a = 0;   
  22.         int k = 0;   
  23.         String s = "";   
  24.         while ((a = sis.readLine(b, 0, b.length)) != -1) {   
  25.             s = new String(b, 0, a);   
  26.             if ((k = s.indexOf("filename=")) != -1) {   
  27.                 s = s.substring(k + 10);   
  28.                 k = s.indexOf("");   
  29.                 s = s.substring(0, k);   
  30.                 sourcefile[count] = s;   
  31.   
  32.                 k = s.lastIndexOf(".");   
  33.                 suffix[count] = s.substring(k + 1);   
  34.                 System.out.println(suffix[count]);   
  35.                 if (canTransfer(count))   
  36.                     transferfile(count);   
  37.             }   
  38.             if (!successful)   
  39.                 break;   
  40.         }   
  41.     }   
  42.   
  43.     public int getCount() {   
  44.         return count;   
  45.     }   
  46.   
  47.     public String[] getSourcefile() {   
  48.         return sourcefile;   
  49.     }   
  50.   
  51.     public void setObjectpath(String objectpath) {   
  52.         this.objectpath = objectpath;   
  53.     }   
  54.   
  55.     public String getObjectpath() {   
  56.         return objectpath;   
  57.     }   
  58.   
  59.     private boolean canTransfer(int i) {   
  60.         suffix[i] = suffix[i].toLowerCase();   
  61.         // 这个是我用来传图片的,各位可以把后缀名改掉或者不要这个条件   
  62.         if (sourcefile[i].equals("")   
  63.                 || (!suffix[i].equals("gif") && !suffix[i].equals("jpg") && !suffix[i]   
  64.                         .equals("jpeg"))) {   
  65.             description[i] = "ERR suffix is wrong";   
  66.             return false;   
  67.         } else  
  68.             return true;   
  69.     }   
  70.   
  71.     private void transferfile(int i) {   
  72.         String x = Long.toString(new java.util.Date().getTime());   
  73.         try {   
  74.             objectfilename[i] = x + "." + suffix[i];   
  75.             FileOutputStream out = new FileOutputStream(objectpath   
  76.                     + objectfilename[i]);   
  77.             int a = 0;   
  78.             int k = 0;   
  79.             long hastransfered = 0;// 标示已经传输的字节数   
  80.             String s = "";   
  81.             while ((a = sis.readLine(b, 0, b.length)) != -1) {   
  82.                 s = new String(b, 0, a);   
  83.                 if ((k = s.indexOf("Content-Type:")) != -1)   
  84.                     break;   
  85.             }   
  86.             sis.readLine(b, 0, b.length);   
  87.             while ((a = sis.readLine(b, 0, b.length)) != -1) {   
  88.                 s = new String(b, 0, a);   
  89.                 if ((b[0] == 45) && (b[1] == 45) && (b[2] == 45)   
  90.                         && (b[3] == 45) && (b[4] == 45))   
  91.                     break;   
  92.                 out.write(b, 0, a);   
  93.                 hastransfered += a;   
  94.                 if (hastransfered >= size) {   
  95.                     description[count] = "ERR The file "  
  96.                             + sourcefile[count]   
  97.                             + " is too large to transfer. The whole process is interrupted.";   
  98.                     successful = false;   
  99.                     break;   
  100.                 }   
  101.             }   
  102.             if (successful)   
  103.                 description[count] = "Right The file " + sourcefile[count]   
  104.                         + " has been transfered successfully.";   
  105.             ++count;   
  106.             out.close();   
  107.             if (!successful) {   
  108.                 sis.close();   
  109.                 File tmp = new File(objectpath + objectfilename[count - 1]);   
  110.                 tmp.delete();   
  111.             }   
  112.         } catch (IOException ioe) {   
  113.             description[i] = ioe.toString();   
  114.         }   
  115.   
  116.     }   
  117.   
  118.     public transfer_multi() {   
  119.         // 可以在构建器里面构建服务器上传目录,也可以在javabean调用的时候自己构建   
  120.         setObjectpath("/home/www/jspvhost4/web/popeyelin/images/");   
  121.     }   
  122. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值