Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package it.grabz.grabzit;
  
  import java.io.File;
 import java.net.URL;

Author(s):
GrabzIt
 
 class Post 
 {
     private final String boundary;
     private static final String LINE_FEED = "\r\n";
     private HttpURLConnection httpConn;
     private String charset;
     private OutputStream outputStream;
     private PrintWriter writer;
 
    
This constructor initializes a new HTTP POST request with content type is set to multipart/form-data

Parameters:
requestURL
charset
Throws:
java.io.IOException
 
     public Post(String requestURLString charset)
             throws IOExceptionException 
     {
         this. = charset;
          
         // creates a unique boundary based on time stamp
          = "===" + System.currentTimeMillis() + "===";
          
         URL url = new URL(requestURL);
         URLConnection connection = (URLConnectionurl.openConnection();
          = (HttpURLConnectionconnection;
         .setDoOutput(true); // indicates POST method
         .setDoInput(true);
         .setRequestProperty("Content-Type",
                 "multipart/form-data; boundary=" + );
          = .getOutputStream();
          = new PrintWriter(new OutputStreamWriter(charset),
                 true);
     }
 
    
Adds a form field to the request

Parameters:
name field name
value field value
 
     public void addFormField(String nameString value
     {
         .append("--" + ).append();
         .append("Content-Disposition: form-data; name=\"" + name + "\"")
                 .append();
         .append("Content-Type: text/plain; charset=" + ).append(
                 );
         .append();
         .append(value).append();
         .flush();
     }
 
    
Adds a upload file section to the request

Parameters:
fieldName name attribute in <input type="file" name="..." />
uploadFile a File to be uploaded
Throws:
java.io.IOException
 
     public void addFilePart(String fieldNameFile uploadFile)
             throws IOException 
     {
         String fileName = uploadFile.getName();
         .append("--" + ).append();
         .append(
                 "Content-Disposition: form-data; name=\"" + fieldName
                         + "\"; filename=\"" + fileName + "\"")
                 .append();
         .append(
                 "Content-Type: "
                         + URLConnection.guessContentTypeFromName(fileName))
                 .append();
         .append("Content-Transfer-Encoding: binary").append();
         .append();
         .flush();
  
         FileInputStream inputStream = new FileInputStream(uploadFile);
         byte[] buffer = new byte[4096];
         int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            .write(buffer, 0, bytesRead);
        }
        .flush();
        inputStream.close();
         
        .append();
        .flush();     
    }
 
    
Completes the request and receives response from the server.

Returns:
a list of Strings as response in case the server returned status OK, otherwise an exception is thrown.
Throws:
java.io.IOException
    public <T> T finish(Class<T> clazzthrows IOExceptionJAXBExceptionException {
        .append().flush();
        .append("--" +  + "--").append();
        .close();
 
        Response response = new Response();        
        return response.Parse(clazz);
    }    
New to GrepCode? Check out our FAQ X