1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
package Admin;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
@SuppressWarnings("deprecation")
public class SampleFileUpload {
/**
* A generic method to execute any type of Http Request and constructs a response object
* @param requestBase the request that needs to be exeuted
* @return server response as <code>String</code>
*/
private static String executeRequest(HttpRequestBase requestBase){
String responseString = "" ;
InputStream responseStream = null ;
HttpClient client = new DefaultHttpClient () ;
try{
HttpResponse response = client.execute(requestBase) ;
if (response != null){
HttpEntity responseEntity = response.getEntity() ;
if (responseEntity != null){
responseStream = responseEntity.getContent() ;
if (responseStream != null){
BufferedReader br = new BufferedReader (new InputStreamReader (responseStream)) ;
String responseLine = br.readLine() ;
String tempResponseString = "" ;
while (responseLine != null){
tempResponseString = tempResponseString + responseLine + System.getProperty("line.separator") ;
responseLine = br.readLine() ;
}
br.close() ;
if (tempResponseString.length() > 0){
responseString = tempResponseString ;
}
}
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (responseStream != null){
try {
responseStream.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}
}
client.getConnectionManager().shutdown() ;
return responseString ;
}
/**
* Method that builds the multi-part form data request
* @param urlString the urlString to which the file needs to be uploaded
* @param file the actual file instance that needs to be uploaded
* @param fileName name of the file, just to show how to add the usual form parameters
* @param fileDescription some description for the file, just to show how to add the usual form parameters
* @return server response as <code>String</code>
*/
public String executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription) {
HttpPost postRequest = new HttpPost (urlString) ;
try{
MultipartEntity multiPartEntity = new MultipartEntity () ;
//The usual form parameters can be added this way
multiPartEntity.addPart("fileDescription", new StringBody(fileDescription != null ? fileDescription : "")) ;
multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName())) ;
/*Need to construct a FileBody with the file that needs to be attached and specify the mime type of the file. Add the fileBody to the request as an another part.
This part will be considered as file part and the rest of them as usual form-data parts*/
FileBody fileBody = new FileBody(file, "application/octect-stream") ;
multiPartEntity.addPart("attachment", fileBody) ;
postRequest.setEntity(multiPartEntity) ;
}catch (UnsupportedEncodingException ex){
ex.printStackTrace() ;
}
return executeRequest (postRequest) ;
}
public static void main(String args[]){
SampleFileUpload fileUpload = new SampleFileUpload () ;
File file = new File ("D:\\ages\\logo_ages.jpg") ;
String response = fileUpload.executeMultiPartRequest("http://127.0.0.1/images/logo_ages.jpg", file, file.getName(), "File Upload test Hydrangeas.jpg description") ;
System.out.println("Response : "+response);
}
} |
Partager