【发布时间】:2018-06-21 11:38:48
【问题描述】:
我一直在尝试使用发布请求将图像上传到网络服务器。为了便于处理,我已将图像更改为 base64 字符串。当我记录 base64 字符串时,我可以看到它大约有 4000-5000 个字符。但是当我在服务器上收到它时,我将它写入一个 txt 文件以查看我得到了什么。但大小超过一百万个字符。只是似乎无法弄清楚出了什么问题。由于某种原因,服务器接收到太多信息。是流的问题还是文件大小的问题?我需要发送完整的图像。
private String encodedImage(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bytes = baos.toByteArray();
String encImage = Base64.encodeToString(bytes,0 ,bytes.length ,Base64.DEFAULT);
return encImage;
}
/**
* On activity result will launch when activity gives a result back.
* @param requestCode, code to check for specific request
* @param resultCode, code to correspond for result
* @param data, intent that gives back result.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == IMAGE_RESULT_CODE && resultCode == RESULT_OK){
// Set the imageview to blank ot fix caching errors
imageView.setImageDrawable(null);
//Get file
File image = new File(Environment.getExternalStorageDirectory(), "StendeNav/Startpunt.jpeg");
Uri imageUri = Uri.fromFile(image);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
} catch (IOException e) {
e.printStackTrace();
}
// set imageview
String base64 = encodedImage(bitmap);
//decode base64 string to image
byte[] imageBytes = Base64.decode(base64, Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
imageView.setImageBitmap(decodedImage);
String urlIn = "http://192.168.43.7/uploadimage.php";
String urlParameters = "image=" + base64;
new uploadBase().execute(urlIn,urlParameters);
}
}
private String httpPOST(String urlIn, String urlParameters, Context context){
StringBuffer data = new StringBuffer("");
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
try{
URL url = new URL(urlIn);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty( "Charset", "UTF-8");
connection.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
try( DataOutputStream wr = new DataOutputStream( connection.getOutputStream())) {
wr.write( postData );
}
InputStream inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
data.append(line);
}
} catch (IOException e) {
// writing exception to log
Log.d("Bullshit",e.toString());
}
return data.toString();
}
public class uploadBase extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
StringBuffer data = new StringBuffer("");
byte[] postData = strings[1].getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
try{
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty( "Charset", "UTF-8");
connection.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
try( DataOutputStream wr = new DataOutputStream( connection.getOutputStream())) {
wr.write( postData );
}
InputStream inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
data.append(line);
}
} catch (IOException e) {
// writing exception to log
Log.d("Bullshit",e.toString());
}
return data.toString(); }
@Override
protected void onPostExecute(String s) {
}
}
【问题讨论】: