ClsMultipartProcess.java
上传用户:mingda
上传日期:2017-06-20
资源大小:27691k
文件大小:4k
源码类别:

OA系统

开发平台:

Java

  1. package com.gforce.currency.clsupload;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. import java.io.*;
  5. import java.util.*;
  6. /**
  7.  * <p>Title: 吉力科技办公自动化系统</p>
  8.  * <p>Description: 吉力科技办公自动化系统</p>
  9.  * <p>Copyright: 版权所有 2003 (c) 西安吉力科技发展有限公司  Copyright (c) 2003 GForce Sceince & Technology</p>
  10.  * <p>Company: 西安吉力科技发展有限公司 (GForce Sceince & Technology)</p>
  11.  * @author 马登军
  12.  * @version 1.0
  13.  */
  14. public class ClsMultipartProcess
  15. {
  16.     private static final String disposition="Content-Disposition: form-data; name="";
  17.     private String boundary=null;
  18.     private String formData=null;
  19.     private int formLength=0;
  20.     public ClsMultipartProcess(HttpServletRequest request)
  21.     {
  22.         try
  23.         {
  24.             DataInputStream in=new DataInputStream(request.getInputStream());
  25.             String contentType=request.getContentType();
  26.             formLength=request.getContentLength();
  27.             if (formLength>0)
  28.             {
  29.                 boundary=contentType.substring(contentType.lastIndexOf("=")+1,
  30.                                                contentType.length());
  31.                 byte buffer[]=new byte[formLength];
  32.                 int readedNumber=0;
  33.                 int totalReadedNumber=0;
  34.                 while(totalReadedNumber<formLength)
  35.                 {
  36.                     readedNumber=in.read(buffer, totalReadedNumber,
  37.                                          formLength-totalReadedNumber);
  38.                     totalReadedNumber+=readedNumber;
  39.                 }
  40.                 formData=new String(buffer, "ISO8859-1");
  41.                 buffer=null;
  42.             }
  43.         }
  44.         catch(IOException ex)
  45.         {
  46.             ex.printStackTrace();
  47.         }
  48.     }
  49.     public String getValue(String controlName)
  50.     {
  51.         String searchStr=disposition+controlName+""";
  52.         String value=null;
  53.         if (formLength>0)
  54.         {
  55.             int startPoint=formData.indexOf(searchStr);
  56.             if (startPoint!=-1)
  57.             {
  58.                 startPoint=formData.indexOf("n", startPoint)+1;
  59.                 startPoint=formData.indexOf("n", startPoint)+1;
  60.                 int endPoint=formData.indexOf(boundary, startPoint)-4;
  61.                 value=formData.substring(startPoint, endPoint);
  62.             }
  63.         }
  64.         return value;
  65.     }
  66.     public String getFileName(String controlName)
  67.     {
  68.         String searchStr=disposition+controlName+""; filename="";
  69.         String fileName=null;
  70.         if (formLength>0)
  71.         {
  72.             int startPoint=formData.indexOf(searchStr);
  73.             int endPoint=formData.indexOf("n", startPoint);
  74.             if (startPoint!=-1)
  75.             {
  76.                 startPoint=startPoint+searchStr.length();
  77.                 fileName=formData.substring(startPoint, endPoint-2);
  78.                 int pos=fileName.lastIndexOf("\");
  79.                 if (pos!=-1)
  80.                 {
  81.                     fileName=fileName.substring(pos+1);
  82.                 }
  83.                 if (fileName!=null)
  84.                 {
  85.                     try
  86.                     {
  87.                         fileName=new String(fileName.getBytes("ISO8859-1"));
  88.                     }
  89.                     catch(UnsupportedEncodingException ex)
  90.                     {
  91.                         ex.printStackTrace();
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.         return fileName;
  97.     }
  98.     public String getFileContent(String controlName)
  99.     {
  100.         String searchStr=disposition+controlName+""; filename="";
  101.         String fileContent=null;
  102.         if (formLength>0)
  103.         {
  104.             int startPoint=formData.indexOf(searchStr);
  105.             if (startPoint!=-1)
  106.             {
  107.                 startPoint=formData.indexOf("n", startPoint)+1;
  108.                 startPoint=formData.indexOf("n", startPoint)+1;
  109.                 startPoint=formData.indexOf("n", startPoint)+1;
  110.                 int endPoint=formData.indexOf(boundary, startPoint)-4;
  111.                 fileContent=formData.substring(startPoint, endPoint);
  112.             }
  113.         }
  114.         return fileContent;
  115.     }
  116. }