亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

http上傳圖片不成功,原因出在哪?

http上傳圖片不成功,原因出在哪?

蔣治國 2017-03-13 19:45:11
http上傳圖片不成功,原因會出在哪?服務器那邊一點反應都沒有?以下是兩個主要文件的代碼:public class UploadThread extends Thread {?private String fileName;??private String url;?public UploadThread(String url,String fileName) {??this.url=url;??this.fileName=fileName;?}??@Override?public void run(){??String boundary="----------------7e1a41281b44";//分界線,不同??String prefix="--";//前綴??String end="\r\n";??try{???URL httpUrl=new URL(url);???HttpURLConnection conn=(HttpURLConnection)httpUrl.openConnection();???conn.setRequestMethod("POST");???conn.setDoOutput(true);???conn.setDoInput(true);//請求屬性???conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);???DataOutputStream out=new DataOutputStream(conn.getOutputStream());???out.writeBytes(prefix+boundary+end);???out.writeBytes("Content-Disposition:form-data;"+"name=\"upload\";filename=\""+"Sky.jpg"+"\""+end); ???out.writeBytes(end);//回車換行符???FileInputStream fileInputStream=new FileInputStream(new File(fileName));???byte[] b=new byte[1024*4];???int len;???while((len=fileInputStream.read(b))!=-1){????out.write(b,0,len);???}???out.writeBytes(end);???out.writeBytes(prefix+boundary+prefix+end);//最后結束行???out.flush();??????BufferedReader reader=new BufferedReader(new InputStreamReader(conn.getInputStream()));???StringBuffer sb=new StringBuffer();???String str;???while((str=reader.readLine())!=null){????sb.append(str);???}??????System.out.println("respone:"+sb.toString());???if(out!=null){????out.close();???}???if(out!=null){????reader.close();???}????????}catch (MalformedURLException e) {???e.printStackTrace();??}catch (IOException e) {???e.printStackTrace();??}?}}public class UploadActivity extends Activity {?private Button button;??@Override?protected void onCreate(Bundle savedInstanceState){??super.onCreate(savedInstanceState);??setContentView(R.layout.upload);//??button=(Button)findViewById(R.id.button_1);??button.setOnClickListener(new View.OnClickListener() {??????@Override???public void onClick(View v) {????// TODO Auto-generated method stub????????String url="http://222.195.117.120:8080/StrutsDay3/upload.action";????File file=Environment.getExternalStorageDirectory();????File upload=new File(file,"Sky.jpg");????????String fileName=upload.getAbsolutePath();????UploadThread thread=new UploadThread(url,fileName);????thread.start();???}??});?}}
查看完整描述

1 回答

?
Freerain

TA貢獻1條經驗 獲得超0個贊

用chales或者finder抓個包看下原因
查看完整回答
反對 回復 2017-03-16
  • 1 回答
  • 0 關注
  • 1296 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號