-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostUpload.java
More file actions
111 lines (91 loc) · 2.87 KB
/
PostUpload.java
File metadata and controls
111 lines (91 loc) · 2.87 KB
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
package testupload;
import java.io.File;
import java.io.IOException;
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.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class PostUpload {
public void uploadedAPKFile(String url, String path)
throws ClientProtocolException, IOException {
File file = new File(path);
if (file.exists() == false) {
System.out.println("failed");
}
HttpClient client = new DefaultHttpClient();
HttpPost postOBJ = new HttpPost(url);
FileBody fBody = new FileBody(file,
"application/vnd.android.package-archive");
MultipartEntity entity = new MultipartEntity();
entity.addPart("application", fBody);
postOBJ.setEntity(entity);
try {
HttpResponse response = client.execute(postOBJ);
int statusCode = response.getStatusLine().getStatusCode();
String result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("statuscode = " + statusCode);
System.out.println("result = " + result);
if (statusCode == 201) {
System.out.println("successed");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
/**
*
* @param url
* @param path
* :file path
* @throws IOException
* @throws ClientProtocolException
*/
public void uploadedImgFile(String url, String path)
throws ClientProtocolException, IOException {
File file = new File(path);
if (file.exists() == false) {
System.out.println("failed");
}
HttpClient client = new DefaultHttpClient();
HttpPost postOBJ = new HttpPost(url);
FileBody fBody = new FileBody(file);
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", fBody);
postOBJ.setEntity(entity);
try {
HttpResponse response = client.execute(postOBJ);
int statusCode = response.getStatusLine().getStatusCode();
String result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("statuscode = " + statusCode);
System.out.println("result = " + result);
if (statusCode == 201) {
System.out.println("successed");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
public static void main(String[] args) {
try {
// new
// PostUpload().uploadedImgFile("http://192.168.1.109/upload_img.php",
// "./a.jpg");
// new
// PostUpload().uploadedImgFile("http://192.168.1.109/upload_img.php",
// "./MainActivity-debug.apk");
new PostUpload().uploadedImgFile(
"http://192.168.1.109/upload.php",
"./MainActivity-debug.apk");
} catch (IOException e) {
e.printStackTrace();
}
}
}