兩種android客戶端傳圖片的方法
阿新 • • 發佈:2019-01-24
///////////2016/03/14///////////
/////////by xbw/////////////////
/////////環境 eclipse php//
第一種,‘
一個類FileUtil
[java] view plain copy print?- package com.example.image_head;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.CompressFormat;
- import android.os.Environment;
- publicclass FileUtil {
- publicstatic String saveFile(Context c, String fileName, Bitmap bitmap) {
- return saveFile(c,
- }
- publicstatic String saveFile(Context c, String filePath, String fileName, Bitmap bitmap) {
- byte[] bytes = bitmapToBytes(bitmap);
- return saveFile(c, filePath, fileName, bytes);
- }
- publicstaticbyte[] bitmapToBytes(Bitmap bm) {
- ByteArrayOutputStream baos = new
- bm.compress(CompressFormat.JPEG, 100, baos);
- return baos.toByteArray();
- }
- publicstatic String saveFile(Context c, String filePath, String fileName, byte[] bytes) {
- String fileFullName = "";
- FileOutputStream fos = null;
- String dateFolder = new SimpleDateFormat("yyyyMMdd", Locale.CHINA)
- .format(new Date());
- try {
- String suffix = "";
- if (filePath == null || filePath.trim().length() == 0) {
- filePath = Environment.getExternalStorageDirectory() + "/JiaXT/" + dateFolder + "/";
- }
- File file = new File(filePath);
- if (!file.exists()) {
- file.mkdirs();
- }
- File fullFile = new File(filePath, fileName + suffix);
- fileFullName = fullFile.getPath();
- fos = new FileOutputStream(new File(filePath, fileName + suffix));
- fos.write(bytes);
- } catch (Exception e) {
- fileFullName = "";
- } finally {
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e) {
- fileFullName = "";
- }
- }
- }
- return fileFullName;
- }
- }
package com.example.image_head;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
public class FileUtil {
public static String saveFile(Context c, String fileName, Bitmap bitmap) {
return saveFile(c, "", fileName, bitmap);
}
public static String saveFile(Context c, String filePath, String fileName, Bitmap bitmap) {
byte[] bytes = bitmapToBytes(bitmap);
return saveFile(c, filePath, fileName, bytes);
}
public static byte[] bitmapToBytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 100, baos);
return baos.toByteArray();
}
public static String saveFile(Context c, String filePath, String fileName, byte[] bytes) {
String fileFullName = "";
FileOutputStream fos = null;
String dateFolder = new SimpleDateFormat("yyyyMMdd", Locale.CHINA)
.format(new Date());
try {
String suffix = "";
if (filePath == null || filePath.trim().length() == 0) {
filePath = Environment.getExternalStorageDirectory() + "/JiaXT/" + dateFolder + "/";
}
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
File fullFile = new File(filePath, fileName + suffix);
fileFullName = fullFile.getPath();
fos = new FileOutputStream(new File(filePath, fileName + suffix));
fos.write(bytes);
} catch (Exception e) {
fileFullName = "";
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
fileFullName = "";
}
}
}
return fileFullName;
}
}
第二個類 NetUtil
[java] view plain copy print?- package com.example.image_head;
- import java.io.ByteArrayOutputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.net.URLEncoder;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- publicclass NetUtil {
- // 一般來說用一個生成一個UUID的話,會可靠很多,這裡就不考慮這個了
- // 而且一般來說上傳檔案最好用BASE64進行編碼,你只要用BASE64不用的符號就可以保證不衝突了。
- // 尤其是上傳二進位制檔案時,其中很可能有\r、\n之類的控制字元,有時還可能出現最高位被錯誤處理的問題,所以必須進行編碼。
- publicstaticfinal String BOUNDARY = "--my_boundary--";
- /**
- * 普通字串資料
- * @param textParams
- * @param ds
- * @throws Exception
- */
- publicstaticvoid writeStringParams(Map<String, String> textParams,
- DataOutputStream ds) throws Exception {
- Set<String> keySet = textParams.keySet();
- for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
- String name = it.next();
- String value = textParams.get(name);
- ds.writeBytes("--" + BOUNDARY + "\r\n");
- ds.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"\r\n");
- ds.writeBytes("\r\n");
- value = value + "\r\n";
- ds.write(value.getBytes());
- }
- }
- /**
- * 檔案資料
- * @param fileparams
- * @param ds
- * @throws Exception
- */
- publicstaticvoid writeFileParams(Map<String, File> fileparams,
- DataOutputStream ds) throws Exception {
- Set<String> keySet = fileparams.keySet();
- for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
- String name = it.next();
- File value = fileparams.get(name);
- ds.writeBytes("--" + BOUNDARY + "\r\n");
- //uploadedfile與伺服器端內容匹配
- ds.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
- + URLEncoder.encode(value.getName(), "UTF-8") + "\"\r\n");
- ds.writeBytes("Content-Type:application/octet-stream \r\n");
- ds.writeBytes("\r\n");
- ds.write(getBytes(value));
- ds.writeBytes("\r\n");
- }
- }
- // 把檔案轉換成位元組陣列
- privatestaticbyte[] getBytes(File f) throws Exception {
- FileInputStream in = new FileInputStream(f);
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- byte[] b = newbyte[1024];
- int n;
- while ((n = in.read(b)) != -1) {
- out.write(b, 0, n);
- }
- in.close();
- return out.toByteArray();
- }
- /**
- * 新增結尾資料
- * @param ds
- * @throws Exception
- */
- publicstaticvoid paramsEnd(DataOutputStream ds) throws Exception {
- ds.writeBytes("--" + BOUNDARY + "--" + "\r\n");
- ds.writeBytes("\r\n");
- }
- publicstatic String readString(InputStream is) {
- returnnew String(readBytes(is));
- }
- publicstaticbyte[] readBytes(InputStream is) {
- try {
- byte[] buffer = newbyte[1024];
- int len = -1;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- while ((len = is.read(buffer)) != -1) {
- baos.write(buffer, 0, len);
- }
- baos.close();
- return baos.toByteArray();
- } catch (Exception e) {
- e.printStackTrace();
- }
- returnnull;
- }
- }
package com.example.image_head;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class NetUtil {
// 一般來說用一個生成一個UUID的話,會可靠很多,這裡就不考慮這個了
// 而且一般來說上傳檔案最好用BASE64進行編碼,你只要用BASE64不用的符號就可以保證不衝突了。
// 尤其是上傳二進位制檔案時,其中很可能有\r、\n之類的控制字元,有時還可能出現最高位被錯誤處理的問題,所以必須進行編碼。
public static final String BOUNDARY = "--my_boundary--";
/**
* 普通字串資料
* @param textParams
* @param ds
* @throws Exception
*/
public static void writeStringParams(Map<String, String> textParams,
DataOutputStream ds) throws Exception {
Set<String> keySet = textParams.keySet();
for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
String name = it.next();
String value = textParams.get(name);
ds.writeBytes("--" + BOUNDARY + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"\r\n");
ds.writeBytes("\r\n");
value = value + "\r\n";
ds.write(value.getBytes());
}
}
/**
* 檔案資料
* @param fileparams
* @param ds
* @throws Exception
*/
public static void writeFileParams(Map<String, File> fileparams,
DataOutputStream ds) throws Exception {
Set<String> keySet = fileparams.keySet();
for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
String name = it.next();
File value = fileparams.get(name);
ds.writeBytes("--" + BOUNDARY + "\r\n");
//uploadedfile與伺服器端內容匹配
ds.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
+ URLEncoder.encode(value.getName(), "UTF-8") + "\"\r\n");
ds.writeBytes("Content-Type:application/octet-stream \r\n");
ds.writeBytes("\r\n");
ds.write(getBytes(value));
ds.writeBytes("\r\n");
}
}
// 把檔案轉換成位元組陣列
private static byte[] getBytes(File f) throws Exception {
FileInputStream in = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
in.close();
return out.toByteArray();
}
/**
* 新增結尾資料
* @param ds
* @throws Exception
*/
public static void paramsEnd(DataOutputStream ds) throws Exception {
ds.writeBytes("--" + BOUNDARY + "--" + "\r\n");
ds.writeBytes("\r\n");
}
public static String readString(InputStream is) {
return new String(readBytes(is));
}
public static byte[] readBytes(InputStream is) {
try {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
方法,在button的點選事件中新增執行緒
- new Thread(uploadImageRunnable).start();
new Thread(uploadImageRunnable).start();
[java]
view plain
copy
print?
- Runnable uploadImageRunnable = new Runnable() {
- @Override
- publicvoid run() {
- if(TextUtils.isEmpty(imgUrl)){//php伺服器端url
- Toast.makeText(getActivity(), "還沒有設定上傳伺服器的路徑!", Toast.LENGTH_SHORT).show();
- return;
- }
- Map<String, String> textParams = new HashMap<String, String>();
- Map<String, File> fileparams = new HashMap<String, File>();
- try {
- // 建立一個URL物件
- URL url = new URL(imgUrl);
- textParams = new HashMap<String, String>();
- fileparams = new HashMap<String, File>();
- // 要上傳的圖片檔案
- File file = new File(urlpath);//本地圖片路徑
- fileparams.put("image", file);
- // 利用HttpURLConnection物件從網路中獲取網頁資料
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- // 設定連線超時(記得設定連線超時,如果網路不好,Android系統在超過預設時間會收回資源中斷操作)
- conn.setConnectTimeout(5000);
- // 設定允許輸出(傳送POST請求必須設定允許輸出)
- conn.setDoOutput(true);
- // 設定使用POST的方式傳送
- conn.setRequestMethod("POST");
- // 設定不使用快取(容易出現問題)
- conn.setUseCaches(false);
- conn.setRequestProperty("Charset", "UTF-8");//設定編碼
- // 在開始用HttpURLConnection物件的setRequestProperty()設定,就是生成HTML檔案頭
- conn.setRequestProperty("ser-Agent", "Fiddler");
- // 設定contentType
- conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + NetUtil.BOUNDARY);
- OutputStream os = conn.getOutputStream();
- DataOutputStream ds = new DataOutputStream(os);
- NetUtil.writeStringParams(textParams, ds);
- NetUtil.writeFileParams(fileparams, ds);
- NetUtil.paramsEnd(ds);
- // 對檔案流操作完,要記得及時關閉
- os.close();
- // 伺服器返回的響應嗎
- int code = conn.getResponseCode(); // 從Internet獲取網頁,傳送請求,將網頁以流的形式讀回來
- // 對響應碼進行判斷
- if (code == 200) {// 返回的響應碼200,是成功
- // 得到網路返回的輸入流
- InputStream is = conn.getInputStream();
- resultStr = NetUtil.readString(is);
- } else {
- Toast.makeText(getActivity(), "請求URL失敗!", Toast.LENGTH_SHORT).show();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- handler.sendEmptyMessage(0);// 執行耗時的方法之後傳送消給handler
- }
- };
Runnable uploadImageRunnable = new Runnable() {
@Override
public void run() {
if(TextUtils.isEmpty(imgUrl)){//php伺服器端url
Toast.makeText(getActivity(), "還沒有設定上傳伺服器的路徑!", Toast.LENGTH_SHORT).show();
return;
}
Map<String, String> textParams = new HashMap<String, String>();
Map<String, File> fileparams = new HashMap<String, File>();
try {
// 建立一個URL物件
URL url = new URL(imgUrl);
textParams = new HashMap<String, String>();
fileparams = new HashMap<String, File>();
// 要上傳的圖片檔案
File file = new File(urlpath);//本地圖片路徑
fileparams.put("image", file);
// 利用HttpURLConnection物件從網路中獲取網頁資料
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 設定連線超時(記得設定連線超時,如果網路不好,Android系統在超過預設時間會收回資源中斷操作)
conn.setConnectTimeout(5000);
// 設定允許輸出(傳送POST請求必須設定允許輸出)
conn.setDoOutput(true);
// 設定使用POST的方式傳送
conn.setRequestMethod("POST");
// 設定不使用快取(容易出現問題)
conn.setUseCaches(false);
conn.setRequestProperty("Charset", "UTF-8");//設定編碼
// 在開始用HttpURLConnection物件的setRequestProperty()設定,就是生成HTML檔案頭
conn.setRequestProperty("ser-Agent", "Fiddler");
// 設定contentType
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + NetUtil.BOUNDARY);
OutputStream os = conn.getOutputStream();
DataOutputStream ds = new DataOutputStream(os);
NetUtil.writeStringParams(textParams, ds);
NetUtil.writeFileParams(fileparams, ds);
NetUtil.paramsEnd(ds);
// 對檔案流操作完,要記得及時關閉
os.close();
// 伺服器返回的響應嗎
int code = conn.getResponseCode(); // 從Internet獲取網頁,傳送請求,將網頁以流的形式讀回來
// 對響應碼進行判斷
if (code == 200) {// 返回的響應碼200,是成功
// 得到網路返回的輸入流
InputStream is = conn.getInputStream();
resultStr = NetUtil.readString(is);
} else {
Toast.makeText(getActivity(), "請求URL失敗!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);// 執行耗時的方法之後傳送消給handler
}
};
[java]
view plain
copy
print?
</pre><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><strong>[java]</strong> <a target=_blank title="view plain" class="ViewSource" href="http://blog.csdn.net/xbw12138/article/details/50890255#">view plain</a><span data-mod="popu_168"> <a target=_blank title="copy" class="CopyToClipboard" href="http://blog.csdn.net/xbw12138/article/details/50890255#">copy</a></span><div style="left: 421px; top: 5638px; width: 18px; height: 18px; position: absolute; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank title="print" class="PrintSource" href="http://blog.csdn.net/xbw12138/article/details/50890255#">print</a></span><a target=_blank title="?" class="About" href="http://blog.csdn.net/xbw12138/article/details/50890255#">?</a><span class="tracking-ad" data-mod="popu_167"><a target=_blank title="在CODE上檢視程式碼片" style="text-indent:0;" href="https://code.csdn.net/snippets/1610066" target="_blank"><img width="12" height="12" style="position:relative;top:1px;left:2px;" alt="在CODE上檢視程式碼片" src="https://code.csdn.net/assets/CODE_ico.png" /></a></span><span class="tracking-ad" data-mod="popu_170"><a target=_blank title="派生到我的程式碼片" style="text-indent:0;" href="https://code.csdn.net/snippets/1610066/fork" target="_blank"><img width="12" height="12" style="position:relative;top:2px;left:2px;" alt="派生到我的程式碼片" src="https://code.csdn.net/assets/ico_fork.svg" /></a></span></div></div><ol class="dp-j"><li class="alt"><span><span>Handler handler = </span><span class="keyword">new</span><span> Handler(</span><span class="keyword">new</span><span> Handler.Callback() { </span></span></li><li><span> <span class="annotation">@Override</span><span> </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">boolean</span><span> handleMessage(Message msg) { </span></span></li><li><span> <span class="keyword">switch</span><span> (msg.what) { </span></span></li><li class="alt"><span> <span class="keyword">case</span><span> </span><span class="number">0</span><span>: </span></span></li><li><span> <span class="comment">//dialogs.dismiss();</span><span> </span></span></li><li class="alt"><span> <span class="keyword">try</span><span> { </span></span></li><li><span> <span class="comment">// 返回資料示例,根據需求和後臺資料靈活處理</span><span> </span></span></li><li class="alt"><span> </span></li><li><span> JSONObject jsonObject = <span class="keyword">new</span><span> JSONObject(resultStr); </span></span></li><li class="alt"><span> String imageUrl = jsonObject.optString(<span class="string">"imageUrl"</span><span>); </span></span></li><li><span> Toast.makeText(getActivity(), imageUrl, Toast.LENGTH_SHORT).show(); </span></li><li class="alt"><span> }<span class="keyword">else</span><span>{ </span></span></li><li><span> Toast.makeText(getActivity(), jsonObject.optString(<span class="string">"statusMessage"</span><span>), Toast.LENGTH_SHORT).show(); </span></span></li><li class="alt"><span> } </span></li><li><span> </span></li><li class="alt"><span> } <span class="keyword">catch</span><span> (JSONException e) { </span></span></li><li><span> e.printStackTrace(); </span></li><li class="alt"><span> } </span></li><li><span> </span></li><li class="alt"><span> <span class="keyword">break</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span> <span class="keyword">default</span><span>: </span></span></li><li><span> <span class="keyword">break</span><span>; </span></span></li><li class="alt"><span> } </span></li><li><span> <span class="keyword">return</span><span> </span><span class="keyword">false</span><span>; </span></span></li><li class="alt"><span> } </span></li><li><span> }); </span></li></ol><div class="save_code tracking-ad" style="display: none;" data-mod="popu_249"><a target=_blank target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png" alt="" /></a></div></div><pre class="java" style="display: none;" name="code" code_snippet_id="1610066" snippet_file_name="blog_20160314_6_9143161">Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 0:
//dialogs.dismiss();
try {
// 返回資料示例,根據需求和後臺資料靈活處理
JSONObject jsonObject = new JSONObject(resultStr);
String imageUrl = jsonObject.optString("imageUrl");
Toast.makeText(getActivity(), imageUrl, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), jsonObject.optString("statusMessage"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
break;
default:
break;
}
return false;
}
});
這樣就好了
第二種。
在button監聽事件中新增
[java] view plain copy print?- new Task().execute();
new Task().execute();
[java]
view plain
copy
print?
- class Task extends AsyncTask<String, Integer, Integer> {
- @Override
- protected Integer doInBackground(String... strings) {
- Integer result = -1;
- try {
- result = UpFile.post(config_a.uploadUrl,
- new File("/storage/extSdCard/b.png"));//本地圖片路徑
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- return result;
- }
- @Override
- protectedvoid onPostExecute(Integer result) {
- Toast.makeText(getActivity(), "" + result, Toast.LENGTH_LONG).show();
- }
- }
class Task extends AsyncTask<String, Integer, Integer> {
@Override
protected Integer doInBackground(String... strings) {
Integer result = -1;
try {
result = UpFile.post(config_a.uploadUrl,
new File("/storage/extSdCard/b.png"));//本地圖片路徑
}
catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
Toast.makeText(getActivity(), "" + result, Toast.LENGTH_LONG).show();
}
}
Upload_image.java類
[java]
view plain
copy
print?
- package com.example.image_head;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.UUID;
- import com.example.configs.config_a;
- import android.widget.Toast;
- publicclass Upload_image {
- publicstaticclass UpFile {
- publicstaticint post(String actionUrl, File file) throws IOException {
- //產生隨機分隔內容
- String BOUNDARY = UUID.randomUUID().toString();
- String PREFIX = "--";
- String LINEND = "\r\n";
- String MULTIPART_FROM_DATA = "multipart/form-data";
- String CHARSET = "UTF-8";
- URL url = new URL(actionUrl);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- //設定超時時間單位是毫秒
- conn.setReadTimeout(5 * 1000);
- //設定允許輸入
- conn.setDoInput(true);
- //設定允許輸出
- conn.setDoOutput(true);
- //不允許使用快取
- conn.setUseCaches(false);
- //設定請求的方法為Post
- conn.setRequestMethod("POST");
- //設定維持長連線
- conn.setRequestProperty("Connection", "keep-alive");
- //設定字符集為UTF-8
- conn.setRequestProperty("Charset", CHARSET);
- //設定檔案的型別
- conn.setRequestProperty("Content-type", MULTIPART_FROM_DATA + "; boundary=" + BOUNDARY);
- DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
- //傳送檔案資料
- if (file != null) {
- StringBuilder sb = new StringBuilder();
- sb.append(PREFIX);
- sb.append(BOUNDARY);
- sb.append(LINEND);
- //uploadedfile與伺服器端內容匹配
- sb.append("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + file.getName() + "\"" + LINEND);
- //
- sb.append("Content-Type: image/png" + LINEND);
- sb.append(LINEND);
- //寫入輸出流中
- outStream.write(sb.toString().getBytes());
- //將檔案讀入輸入流中
- InputStream is = new FileInputStream(file);
- byte[] buffer = newbyte[1024];
- int len = -1;
- //寫入輸出流中
- while ((len = is.read(buffer)) != -1) {
- outStream.write(buffer, 0, len);
- }
- is.close();
- //新增換行標識
- outStream.write(LINEND.getBytes());
- }
- byte[] endData = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
- outStream.write(endData);
- //傳送資料
- outStream.flush();
- //獲取響應碼 上傳成功返回的是200
- int res = conn.getResponseCode();
- return res;
- }
- }
- }
package com.example.image_head;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOExcep