我尝试使用php将图像上传到服务器。但是我收到了以下错误:
引起:java.lang.IllegalArgumentException:索引171处查询中的非法字符:我的服务器名称/ upload.php?sel_bar_code = ab12& text = hello& image = [编码图像到base64听到]
我在Android上没有收到此错误。下面是我的代码,它将图像上传到服务器。
if (isImage) {
imgPreview.setVisibility(View.VISIBLE);
vidPreview.setVisibility(View.GONE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// down sizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
bitmap = BitmapFactory.decodeFile(filePath, options);
imgPreview.setImageBitmap(bitmap);
ByteArrayOutputStream ByteStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, ByteStream);
byte [] b=ByteStream.toByteArray();
image_value=Base64.encodeToString(b, Base64.DEFAULT);
后台流程代码:
protected String doInBackground(Void... params) {
return uploadFile();
}
@SuppressWarnings("deprecation")
private String uploadFile() {
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
// These params should ordered in key
qparams.add(new BasicNameValuePair("sel_bar_code", product_id));
HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL+"sel_bar_code="+product_id+"&text="+text_value +"&image="+image_value);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(filePath);
// Adding file data to http body
entity.addPart("image", new FileBody(sourceFile));
// Extra parameters if you want to pass to server
entity.addPart("website",
new StringBody("www.google.com"));
entity.addPart("email", new StringBody("info@gmail.com"));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
答案 0 :(得分:0)
昨天我开发了相同的流程...
我的应用程序的差异需要逐个发送照片。另一方面,我发送图像字节为String,最后我遇到了Base64压缩问题,因为标志'Base64.DEFAULT'在我的情况下在字符串中添加了intros。我使用标志'Base64.NO_WRAP'来解决这个问题。为了省略此问题,您需要在发送之前将文件复制到公共目录以验证文件是否已损坏。
服务器具有可以发送的低大小文件也是可行的,服务器部分限制了接收文件的大小。
如果我能帮助你告诉我和好的节目!