上载到服务器后的图像文件为空

吸吮

我正在尝试将图像上传到服务器。

这是我在Android中编写的用于将图像文件以及其他一些参数发送到服务器的代码:

static String imagePath = "/storage/sdcard0/Pictures/image.jpg";
static String url = "http://example.com/api";
static String user_id = "99401";

public static void executeMultipartPost() throws IOException, ClientProtocolException {

    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost(url);
    File file = new File(imagePath);
    FileBody fb = new FileBody(file);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    builder.addPart("user_id", new StringBody(user_id));
    builder.addPart("type", new StringBody("single"));
    builder.addPart("userfile", fb);
    final HttpEntity entity = builder.build();


    httppost.setEntity(entity);

    Log.i(TAG, "Executing request: " + httppost.getRequestLine());

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();
    Log.i(TAG, ""+response.getStatusLine());
    if (resEntity != null) {
        Log.i(TAG, EntityUtils.toString(resEntity));
    }
    if (resEntity != null) {
        resEntity.consumeContent();
    }
    httpclient.getConnectionManager().shutdown();
}

一切正常,除了上载到服务器时,服务器接收到一个映像。我只能看到一个0字节的图像文件。我猜客户端代码中的某些内容设置不正确。

执行代码后,我在Logcat中得到的响应是这样的(来自Laravel错误日志):

<html><h2>Unhandled Exception</h2>
    <h3>Message:</h3>
    <pre>copy(): The first argument to copy() function cannot be a directory</pre>
    <h3>Location:</h3>
    <pre>/home/ked.ai/www/laravel/file.php on line 92</pre>
    <h3>Stack Trace:</h3>
    <pre>#0 /home/ked.ai/www/laravel/laravel.php(42): Laravel\Error::native(2, 'copy(): The fir...', '/home/ked.ai/ww...', 92)
    #1 [internal function]: Laravel\{closure}(2, 'copy(): The fir...', '/home/ked.ai/ww...', 92, Array)
    #2 /home/ked.ai/www/laravel/file.php(92): copy('/', '/home/ked.ai/ww...')
    #3 /home/ked.ai/www/application/controllers/api2/item.php(190): Laravel\File::copy('/', '/home/ked.ai/ww...')
    #4 [internal function]: Api2_Item_Controller->post_new()
    #5 /home/ked.ai/www/laravel/routing/controller.php(325): call_user_func_array(Array, Array)
    #6 /home/ked.ai/www/laravel/routing/controller.php(285): Laravel\Routing\Controller->response('new', Array)
    #7 /home/ked.ai/www/laravel/routing/controller.php(165): Laravel\Routing\Controller->execute('new', Array)
    #8 /home/ked.ai/www/laravel/routing/route.php(153): Laravel\Routing\Controller::call('api2.item@new', Array)
    #9 /home/ked.ai/www/laravel/routing/route.php(124): Laravel\Routing\Route->response()
    #10 /home/ked.ai/www/laravel/laravel.php(167): Laravel\Routing\Route->call()
    #11 /home/ked.ai/www/public/index.php(34): require('/home/ked.ai/ww...')
    #12 {main}</pre></html>

似乎它传递的是目录而不是文件任何解决方法?

吸吮

我设法找到了解决方案。

我只需要改变这条线

builder.addPart("userfile", fb);

builder.addPart("userfile[]", fb);

因为在服务器端,$_FILES['userfile']接收数组类型。

谢谢你。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将图像文件从IOS上载到Web服务器

如何从服务器上载和查看图像文件

图像文件成功在localhost中上载,但不能在使用CI的服务器中上载

将从画布获得的图像上载到服务器

上载到服务器之前裁剪图像

上载到服务器之前优化图像的最佳方法

使用图像文件文件夹从数据库过滤图像文件名后从服务器发送图像文件

上载到服务器时文件代码格式丢失

将录制的文件上载到服务器代号一

CollectionsFS文件未上载到服务器

iOS上的Safari无法Ajax上载某些图像文件:“无法连接到服务器”(但网站正常)

Filezilla上载到服务器错误

上载到跳过Web服务器

图像文件为空

获取存储在解析服务器上的图像文件

如何从服务器正确获取图像文件?

图像裁剪后如何将base 64转换为图像文件,并需要上传服务器

解析服务器-如何使用云代码从服务器删除图像文件

在上载到iOS上的服务器之前调整图像大小

Android Studio:如何在上载到服务器之前调整图像大小?

在上载到android上的ftp服务器时,FTPClient损坏图像吗?

如何在上载到服务器之前调整$ cordovaCapture.captureImage图像的大小?

将多个图像字段上载到服务器mysql中的每一行

将Adobe Creative编辑的图像而不是其Creative Cloud上载到我的服务器

使用Retrofit将文件上载到JSON的文件上传到服务器

r Shiny-将所有文件从ShinyDirChoose文件夹上载到服务器

服务器端/非交互式将图表从JFreeChart渲染为图像文件

上载到服务器时,字体真棒图标为正方形

未上载到服务器和变量的文件被称为未定义