Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在上载到服务器之前更改文件名_Android_Image_File - Fatal编程技术网

Android 在上载到服务器之前更改文件名

Android 在上载到服务器之前更改文件名,android,image,file,Android,Image,File,我需要用一个特定的名称将一个图像上传到我的服务器上,但理想情况下,我仍然希望以原始文件名保存存储在设备上的图像。这就是我所尝试的: myImageFile.renameTo(new File("mobileimage.jpg")); 但是当文件上传到服务器时,它似乎没有我的新名字。以下是将映像上载到服务器的完整代码: DefaultHttpClient mHttpClient; HttpParams params = new BasicHttpParams();

我需要用一个特定的名称将一个图像上传到我的服务器上,但理想情况下,我仍然希望以原始文件名保存存储在设备上的图像。这就是我所尝试的:

myImageFile.renameTo(new File("mobileimage.jpg"));
但是当文件上传到服务器时,它似乎没有我的新名字。以下是将映像上载到服务器的完整代码:

        DefaultHttpClient mHttpClient;
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    mHttpClient = new DefaultHttpClient(params);

    try {
        myImageFile.renameTo(new File("mobileimage.jpg"));

        HttpPost httppost = new HttpPost("http://mywebsite/mobile/image");

        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
        multipartEntity.addPart("userID", new StringBody(Constants.userID));
        multipartEntity.addPart("uniqueMobileID", new StringBody(Constants.uniqueMobileID));
        multipartEntity.addPart("userfile", new FileBody(myImageFile));
        httppost.setEntity(multipartEntity);

        HttpResponse response = mHttpClient.execute(httppost);
        String responseBody = EntityUtils.toString(response.getEntity());

        Log.d(TAG, "response: " + responseBody);
        return responseBody;

    } catch (Exception e) {
       Log.d(TAG, e.getMessage());
    }

如何更改文件名?

请使用将文件名作为参数的文件体格式

在附加文件时尝试使用的其他实现,以便您可以将文件名字段添加到HTTP请求中。大概是这样的:

FormBodyPart userFile = new FormBodyPart("userfile", new FileBody(myImageFile));
userFile.addField("filename","NEWNAMEOFILE.jpg");
multipartEntity.addPart(userFile);
renameTo()是否可能返回false?您应该检查返回值,尤其是使用renameTo()