你好
我尝试通过HTTP服务器将文件上传到android,
我已经制作了HTML表单上传文件,但是如何接收该文件存储在SDCard中?
我只是接收文件名,没有别的。
如何接收该文件?
答案 0 :(得分:0)
MultipartEntity entity = new MultipartEntity();
entity.addPart("type", new StringBody("photo"));
entity.addPart("data", new FileBody(new File(filepath));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
比阅读回复 继承人参考here
或使用没有库多部分的此示例 here
答案 1 :(得分:0)
private void uploadFile(File file) throws IOException {
Log.i(TAG, "Uploading " + file);
String videoName = file.getParentFile().getName();
AndroidHttpClient httpclient = AndroidHttpClient.newInstance(GoProLive.TAG);
try {
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), ConnectTimeout);
HttpConnectionParams.setSoTimeout(httpclient.getParams(), DataTimeout);
HttpPost post = new HttpPost(
String.format("http://" + ServerName + "/upload/%s/%s", user.getUsername(), file.getName()));
post.setEntity(new FileEntity(file, "application/octet-stream"));
SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT, Locale.US);
df.applyPattern("EEE, dd MMM yyyy HH:mm:ss z");
post.setHeader("Last-Modified", df.format(new Date(file.lastModified())));
HttpResponse httpResponse = executePost(httpclient, post);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
file.delete();
} else {
throw new HttpException("Failed to upload file " + file.getAbsolutePath(), statusCode);
}
} finally {
httpclient.close();
}
}