【发布时间】:2016-09-15 05:37:48
【问题描述】:
我正在尝试使用 Codename One 构建一个应用程序,该应用程序将从相机拍摄的照片上传到服务器,然后连同 GPS 坐标一起缩小到 300x300。
在模拟器(Iphone 或 AAndroid)上一切正常,照片被接收并存储在服务器上,其他数据也是如此。构建 Android 应用后,它也可以在 Android 上正常运行。
但是,在构建 iOS 应用程序并在我的 Iphone 4 上对其进行测试时,从未收到过照片。事实上,数据没有通过 Laravel 中的验证器:
$validator = Validator::make($submittedData->all(), [
'longitude' => 'required|numeric',
'latitude' => 'required|numeric',
'accuracy' => 'required|numeric',
'pic' => 'required|image|mimes:jpeg|max:100',
]);
通常接收到的文件大小在 10k 左右,但根据 laravel 的日志报告,这里小于 1k:
[2016-05-18 15:27:10] local.INFO: POST /public/www/API/storeAPI HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: fr-fr
Connection: close
Content-Length: 518
Content-Type: multipart/form-data; boundary=154c47a4886
Cookie: cluster=R2881455720
Host: lambda.fr
Remote-Ip: x.y.z.a
User-Agent: MyApp/2.8 CFNetwork/672.1.15 Darwin/14.0.0
X-Predictor: 1
所以我不确定是我在 Codename One 中做错了什么,还是与 Laravel 有关。
在 CN1 中我使用以下方式发送数据:
`MultipartRequest requete = new MultipartRequest();
requete.setUrl(URL_DEST);
requete.setTimeout(ParametresGeneraux.REQUEST_TIMEOUT);
requete.setPost(true);
requete.setFailSilently(true);
requete.setSilentRetryCount(ParametresGeneraux.SEND_NUMBER_OF_RETRY);
requete.addData("pic", storageDir + imgPath, "image/jpeg");
requete.addArgument("latitude", Double.toString(location.getLatitude()));
requete.addArgument("longitude", Double.toString(location.getLongitude()));
requete.addArgument("accuracy", Double.toString(location.getAccuracy()));
NetworkManager.getInstance().addToQueueAndWait(requete);`
任何帮助将不胜感激,使图片上传也适用于 Iphone 设备!
编辑 1:看来我发送的调整大小的图像(上面的“storageDir + imgPath”)在我的 Iphone 上的长度为 0B(尽管它适用于 Android 和模拟器)。因此,我将仔细研究从 StateMachine 启动的调整大小方法,如下所示。
编辑 2:如果我添加给出文件长度的行,我会在模拟器中得到一个值,但在真正的 Iphone 4 设备上会得到 0。因此,我最初的问题与上传文件无关,而是与路径有关。我应该打开一个新问题吗?
`public static final boolean resizeImageFile (Image image, String outpath, String outFormat, final int width, final int height) {
Image scaledImage = image.scaled(width, height);
// Par défaut on prend le format jpeg
if(!(outFormat.equals(ImageIO.FORMAT_JPEG) || outFormat.equals(ImageIO.FORMAT_PNG)) ){
outFormat = ImageIO.FORMAT_JPEG;
}
try {
OutputStream os = Storage.getInstance().createOutputStream(outpath);
if ( scaledImage != null ) { /*
* ATTENTION le paramètre de qualité ne semble pas être pris en compte
*/
ImageIO.getImageIO().save(scaledImage, os, outFormat, ParametresGeneraux.getPicQuality());
os.close();
/*为编辑 2 添加的行 */ Dialog.show("保存文件的大小", FileSystemStorage.getInstance().getLength(HOME_DIR + 文件名) + "B", "OK", null); // 这在模拟器上显示 xxxB,在设备(Iphone 4)上显示 0B
return true;
}
else {
return false;
}
} catch (IOException e) {
return false;
}
}`
编辑 3:最后问题已通过以下方式解决。而不是使用 Storage.getInstance().createOutputStream(outpath);它将文件(此处为输出路径)存储在存储中的某处,我切换到 OutputStream os = FileSystemStorage.getInstance().openOutputStream(outpath);由于 FileSystemStorage 只处理绝对路径,如果 outpath = FileSystemStorage.getInstance().getAppHomePath + aFileNameOfYourChoice 那么你可以发送数据 requete.addData("pic", FileSystemStorage.getInstance().getAppHomePath + aFileNameOfYourChoice, "image/jpeg");
问候
【问题讨论】:
-
验证者:
mimes:jpeg... 请求:text/plain。您希望它如何工作? -
没错。最初在 Codename One 的 addData 方法中有“image/jpeg”,但我在文档和最近的一篇文章 [link] (mzan.com/article/…) 中看到 Shai 使用“text-plain”作为 mime。这就是我尝试的原因。无论如何,它没有改变任何东西,你是对的,为了清楚起见,我应该更正它。
-
仅供参考,我从未听说过该域名,我猜他们偷了我在其他地方给出的回复,可能缺少其他 cmets
标签: laravel codenameone