我使用以下代码将图像发送到网络服务器。在字符串视图状态中,我得到“/ wEPDwUKLTQwMjY2MDA0M2RkXtxyHItfb0ALigfUBOEHb / mYssynfUoTDJNZt / K8pDs =”作为响应。但我想要的URL。我怎样才能做到这一点。
@Override
protected String doInBackground(String... params) {
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
//System.out.println("byte array:"+image);
String img_str = Base64.encodeToString(image, 0);
//System.out.println("string:"+img_str);
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "********";
String response = "";
int maxBufferSize = 1*1024*1024;
String mimeType = "image/jpeg";
URL url;
try {
url = new URL("url");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
//conn.setChunkedStreamingMode(0);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
DataOutputStream dos;
dos = new DataOutputStream(conn.getOutputStream());
// dos.writeBytes("Content-Disposition: form-data; name=\"__VIEWSTATE\"\r\n\r\n" );
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + img_str +"\"" + lineEnd);
dos.writeBytes(lineEnd);
//FileInputStream fileInputStream = new FileInputStream(img_str);
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
s=b.toString();
Log.i("Response",s);
dos.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
@Override
protected void onPostExecute(String s) {
int i = 0;
String viewstate = "";
while (true){
int found = s.indexOf("\"__VIEWSTATE\"", i);
if (found == -1) break;
int start = found + 38; //check numbers from start of "__V"
int end = (s.indexOf("/>", start)) -2;
viewstate = s.substring(start, end);
i = end + 1;
Log.e("VIEW STATE", viewstate);
StringBuilder builder=new StringBuilder();
builder.append(image);
builder.append(viewstate);
String a= builder.toString();
System.out.println(a);
super.onPostExecute(s);
}
答案 0 :(得分:0)
您可以发布示例网址吗?
编辑:int found = s.indexOf(“\”__ VIEWSTATE \“”,i);返回第一个匹配模式的索引。所以,如果你不想获取网址,你应该使用viewstate = s.substring(0,found);