你好
我试图通过 HTTP 服务器将文件上传到 android,
我已经制作了 HTML 表单来上传文件,但是如何接收该文件以存储在 SDCard 中?
我只是收到文件名,没有别的。
如何接收该文件?
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();
}
}