http上传图片不成功,原因出在哪?

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();
   }
  });
 }
}



蒋治国
浏览 1255回答 1
1回答

Freerain

用chales或者finder抓个包看下原因
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android