0

你好

我试图通过 HTTP 服务器将文件上传到 android,

我已经制作了 HTML 表单来上传文件,但是如何接收该文件以存储在 SDCard 中?

我只是收到文件名,没有别的。

如何接收该文件?

4

2 回答 2

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();
    }
}
于 2013-12-21T11:17:31.493 回答
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);

比阅读这里的回复这里的参考

或在此处使用没有库多部分的此示例

于 2013-04-19T13:17:00.900 回答