代码:
File f = new File("E:\\image.png");
FileInputStream is = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.framesocket.com/api/media/upload.php");
MultipartEntity multipartEntity = new MultipartEntity();
String fsKey = "***";
String fsSecret = "***";
String fsGatekeeper = "***";
String mediaFilename = "test";
JSONObject resultJson;
try {
// Add authentication variables.
multipartEntity.addPart("key", new StringBody(fsKey));
multipartEntity.addPart("secret", new StringBody(fsSecret));
multipartEntity.addPart("sig", new StringBody(md5(fsGatekeeper + "upload")));
// Optionally add title, description, etc.
multipartEntity.addPart("title", new StringBody("Test"));
// Add the media file:
multipartEntity.addPart("media", new InputStreamBody(is = new FileInputStream(f), "E:\\image.png"));
// Execute POST request.
httpPost.setEntity(multipartEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
// Handle Response
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = httpEntity.getContent();
String resultString = convertStreamToString(inputStream);
resultJson = new JSONObject(resultString);
} catch (Exception e) {
e.printStackTrace();
}
HTML的形式:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="UploadServlet" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
我编写Web应用程序,将文件上传到framesocket.com。我上传了具有特定路径的文件,例如:E:\\image.png
。但我希望上传文件使用input type="file"
或任何其他方式动态地从用户机器。这有多容易吗?有什么想法吗?