上传图片,不成功啊?没反应
上传图片,不成功啊?没反应
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();