http要求----ファイルアップロードツール

16957 ワード

Javaコード 
public class FormFile {   
     //           
     private byte[] data;   
  
    private InputStream inStream;   
  
   //        
   private String filename;   
    //        
    private String formname;   
    //        
   private String contentType = "application/octet-stream";   
  
   public FormFile(String filename, byte[] data, String formname,   
            String contentType) {   
        this.data = data;   
       this.filename = filename;   
        this.formname = formname;   
        if (contentType != null) {   
            this.contentType = contentType;   
        }   
    }   
  
  public FormFile(String filename, InputStream inStream, String formname,   
            String contentType) {   
       this.filename = filename;   
       this.formname = formname;   
        this.inStream = inStream;   
        if (contentType != null) {   
            this.contentType = contentType;   
        }   
   }   
  
    public byte[] getData() {   
        return data;   
    }   
  
    public void setData(byte[] data) {   
       this.data = data;   
    }   
  
    public InputStream getInStream() {   
        return inStream;   
    }   
  
    public void setInStream(InputStream inStream) {   
        this.inStream = inStream;   
    }   
  
   public String getFilename() {   
       return filename;   
   }   
  
    public void setFilename(String filename) {   
        this.filename = filename;   
    }   
  
    public String getFormname() {   
        return formname;   
    }   
  
    public void setFormname(String formname) {   
        this.formname = formname;   
    }   
  
    public String getContentType() {   
        return contentType;   
    }   
  
   public void setContentType(String contentType) {   
        this.contentType = contentType;   
    }   
}  
 public class FormFile {
      //        
      private byte[] data;
 
     private InputStream inStream;

    //     
    private String filename;
     //     
     private String formname;
     //     
    private String contentType = "application/octet-stream";
 
    public FormFile(String filename, byte[] data, String formname,
             String contentType) {
         this.data = data;
        this.filename = filename;
         this.formname = formname;
         if (contentType != null) {
             this.contentType = contentType;
         }
     }
 
   public FormFile(String filename, InputStream inStream, String formname,
             String contentType) {
        this.filename = filename;
        this.formname = formname;
         this.inStream = inStream;
         if (contentType != null) {
             this.contentType = contentType;
         }
    }
 
     public byte[] getData() {
         return data;
     }
 
     public void setData(byte[] data) {
        this.data = data;
     }

     public InputStream getInStream() {
         return inStream;
     }

     public void setInStream(InputStream inStream) {
         this.inStream = inStream;
     }
 
    public String getFilename() {
        return filename;
    }
 
     public void setFilename(String filename) {
         this.filename = filename;
     }
 
     public String getFormname() {
         return formname;
     }

     public void setFormname(String formname) {
         this.formname = formname;
     }
 
     public String getContentType() {
         return contentType;
     }

    public void setContentType(String contentType) {
         this.contentType = contentType;
     }
 } 
Javaコード 
public class HttpUploadRequester {   
    /**  
     *     HTTP          ,           : <FORM METHOD=POST  
    * ACTION="http://192.168.0.200:8080/ssi/fileload/test.do"  
      * enctype="multipart/form-data"> <INPUT TYPE="text" NAME="name"> <INPUT  
      * TYPE="text" NAME="id"> <input type="file" name="imagefile"/> <input  
      * type="file" name="zip"/> </FORM>  
    *   
     * @param actionUrl  
      *                ( :    localhost 127.0.0.1       ,           ,  
       *                 http://192.168.1.10:8080       )  
       * @param params  
      *                 key    ,value      
       * @param file  
       *                  
       */  
      // http      ,       
      private static final String HTTP_BOUNDARY = "---------9dx5a2d578c2";   
      private static final String MULTIPART_FORM_DATA = "multipart/form-data";   
      private static final String LINE_ENTER = "\r
"; // private static final int RESPONSE_OK = 200; public static String post(String urlPath, Map<String, String> params, FormFile[] formFiles) { try { URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setReadTimeout(5 * 1000); conn.setDoOutput(true); // POST , conn.setUseCaches(false); conn.setRequestProperty("Connection", "Keep-Alive"); // conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + HTTP_BOUNDARY); StringBuilder formItemData = new StringBuilder(); // for (Map.Entry<String, String> entry : params.entrySet()) { formItemData.append("--"); formItemData.append(HTTP_BOUNDARY); formItemData.append(LINE_ENTER); formItemData.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r
\r
"); formItemData.append(entry.getValue()); formItemData.append(LINE_ENTER); } DataOutputStream outStream = new DataOutputStream( conn.getOutputStream()); // outStream.write(formItemData.toString().getBytes()); // for (FormFile fileData : formFiles) { StringBuilder fileSplit = new StringBuilder(); fileSplit.append("--"); fileSplit.append(HTTP_BOUNDARY); fileSplit.append(LINE_ENTER); fileSplit.append("Content-Disposition: form-data;name=\"" + fileData.getFormname() + "\";filename=\"" + fileData.getFilename() + "\"\r
"); fileSplit.append("Content-Type:" + fileData.getContentType() + LINE_ENTER + LINE_ENTER); outStream.write(fileSplit.toString().getBytes()); if (fileData.getInStream() != null) { byte[] buffer = new byte[1024]; int length = 0; while ((length = fileData.getInStream().read()) != -1) { outStream.write(buffer, 0, length); } fileData.getInStream().close(); } else { outStream.write(fileData.getData(), 0, fileData.getData().length); } outStream.write(LINE_ENTER.getBytes()); } // byte[] endData = ("--" + HTTP_BOUNDARY + "--" + LINE_ENTER) .getBytes(); outStream.write(endData); outStream.flush(); outStream.close(); int responseCode = conn.getResponseCode(); if (responseCode != RESPONSE_OK) { throw new RuntimeException(" url "); } InputStream is = conn.getInputStream(); int ch; StringBuilder b = new StringBuilder(); while ((ch = is.read()) != -1) { b.append((char) ch); } Log.i("HttpPost", b.toString()); conn.disconnect(); return b.toString(); } catch (Exception e) { throw new RuntimeException(); } } // public static String post(String urlPath, Map<String, String> params, FormFile formFiles) { return post(urlPath, params, new FormFile[] { formFiles }); } } public class HttpUploadRequester { /** * HTTP , : <FORM METHOD=POST * ACTION="http://192.168.0.200:8080/ssi/fileload/test.do" * enctype="multipart/form-data"> <INPUT TYPE="text" NAME="name"> <INPUT * TYPE="text" NAME="id"> <input type="file" name="imagefile"/> <input * type="file" name="zip"/> </FORM> * * @param actionUrl * ( : localhost 127.0.0.1 , , * http://192.168.1.10:8080 ) * @param params * key ,value * @param file * */ // http , private static final String HTTP_BOUNDARY = "---------9dx5a2d578c2"; private static final String MULTIPART_FORM_DATA = "multipart/form-data"; private static final String LINE_ENTER = "\r
"; // private static final int RESPONSE_OK = 200; public static String post(String urlPath, Map<String, String> params, FormFile[] formFiles) { try { URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setReadTimeout(5 * 1000); conn.setDoOutput(true); // POST , conn.setUseCaches(false); conn.setRequestProperty("Connection", "Keep-Alive"); // conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + HTTP_BOUNDARY); StringBuilder formItemData = new StringBuilder(); // for (Map.Entry<String, String> entry : params.entrySet()) { formItemData.append("--"); formItemData.append(HTTP_BOUNDARY); formItemData.append(LINE_ENTER); formItemData.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r
\r
"); formItemData.append(entry.getValue()); formItemData.append(LINE_ENTER); } DataOutputStream outStream = new DataOutputStream( conn.getOutputStream()); // outStream.write(formItemData.toString().getBytes()); // for (FormFile fileData : formFiles) { StringBuilder fileSplit = new StringBuilder(); fileSplit.append("--"); fileSplit.append(HTTP_BOUNDARY); fileSplit.append(LINE_ENTER); fileSplit.append("Content-Disposition: form-data;name=\"" + fileData.getFormname() + "\";filename=\"" + fileData.getFilename() + "\"\r
"); fileSplit.append("Content-Type:" + fileData.getContentType() + LINE_ENTER + LINE_ENTER); outStream.write(fileSplit.toString().getBytes()); if (fileData.getInStream() != null) { byte[] buffer = new byte[1024]; int length = 0; while ((length = fileData.getInStream().read()) != -1) { outStream.write(buffer, 0, length); } fileData.getInStream().close(); } else { outStream.write(fileData.getData(), 0, fileData.getData().length); } outStream.write(LINE_ENTER.getBytes()); } // byte[] endData = ("--" + HTTP_BOUNDARY + "--" + LINE_ENTER) .getBytes(); outStream.write(endData); outStream.flush(); outStream.close(); int responseCode = conn.getResponseCode(); if (responseCode != RESPONSE_OK) { throw new RuntimeException(" url "); } InputStream is = conn.getInputStream(); int ch; StringBuilder b = new StringBuilder(); while ((ch = is.read()) != -1) { b.append((char) ch); } Log.i("HttpPost", b.toString()); conn.disconnect(); return b.toString(); } catch (Exception e) { throw new RuntimeException(); } } // public static String post(String urlPath, Map<String, String> params, FormFile formFiles) { return post(urlPath, params, new FormFile[] { formFiles }); } } Java // public static void sendDataToServerByForm() throws Exception { Map<String, String> params = new HashMap<String, String>(); params.put("method", "sendDataByForm"); params.put("strData", " "); // SDCard good.jpg File file = new File(Environment.getExternalStorageDirectory(), "app_Goog_Android_w.png"); FormFile fileData = new FormFile("app_Goog_Android_w.png", new FileInputStream(file), "fileData", "application/octet-stream"); HttpUploadRequester.post( "http://192.168.0.2:8080/AndroidWebServer/server.do", params, fileData); }