为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 1254 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信