上传照片的问题

ya_xiaohu 2011-06-25 10:10:17
各位大侠,我做了一个上传图片,在模拟器上是好的,可是到真机上就上传不了了,这个问题是我很头疼,下面是我的代码,各位大侠帮忙看看 ,谢了……

public String sendFile(String actionUrl, String uploadFile) {
URL url = null;
HttpURLConnection con = null;
try {
url = new URL(actionUrl);
System.out.println("url = " + url);
con = (HttpURLConnection) url.openConnection();
System.out.println("con = " + con);
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
System.out.println("<<<<<<<<<<<<<<<<<<<");
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("enctype","multipart/form-data;boundary=*****");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
/* 设置DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream()); System.out.println("ds = " + ds);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
System.out.println("fStream = " + fStream);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
System.out.println("buffer =" + buffer);
int length = -1;
/* 从文件读取数据至缓冲区 */
while ((length = fStream.read(buffer)) != -1) {
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
System.out.println("==================______________----------");
/* close streams */
fStream.close();
ds.flush();

System.out.println("fdfdf========" + con.getResponseCode());
/* 取得Response内容 */
InputStream is = con.getInputStream();
System.out.println("is = " + is);
BufferedReader sBuffer = new BufferedReader(new InputStreamReader(is));
System.out.println("sBuffer = " + sBuffer);
String response = null;
try {
response = sBuffer.readLine();
System.out.println("上传图片返回的数据"+response);
JSONObject jsonObjectResponse = new JSONObject(response);
String id = jsonObjectResponse.getJSONObject("data").getString("imgid");
System.out.println("imagid = " +id);
sBuffer.close();
return id;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {

}
return null;
}
}


问题是走到DataOutputStream ds = new DataOutputStream(con.getOutputStream());
这块就不走了,我不知道怎么办了,麻烦各位看一下……
...全文
61 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AspireHouse 2011-06-27
  • 打赏
  • 举报
回复
看看你的真机的apn 也许用的是wap就需要设置代理
jeky_zhang2013 2011-06-27
  • 打赏
  • 举报
回复
看下在真机上的调试信息
ya_xiaohu 2011-06-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 aspirehouse 的回复:]
看看你的真机的apn 也许用的是wap就需要设置代理
[/Quote]
但是我用的是wifi啊,这个也需要设置吗?
hello_money 2011-06-27
  • 打赏
  • 举报
回复
不清楚。但还是要顶一下。

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧