在 android 应用程序和 php 服务器站点中上传图片并使用 POST 方法发送一些文本
Upload Image and Send some text with it POST method in android app and php server site
这是自答题
我想发送一些带有要上传的图像的文本,我知道 Apache HttpClient multipart 已被弃用。所以我想用 HTTPUrlConnection 来做到这一点。下面是我的代码,请帮忙。
我得到了答案:我只是重写了我的代码(正确的代码)。希望帮助其他人遇到同样的麻烦。我想发送一张带有用户名和令牌的图片,在 android 和 php 中会像这样:
protected Object doInBackground(Object[] params) {
int fbyte,buffersize,cbuffer;
int maxbuffer=1024*1024;
final String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
BufferedReader reader = null;
try{
String sfile = params[1].toString();
File f=new File(sfile);
FileInputStream fis=new FileInputStream(f);
URL url=new URL(params[0].toString());
HttpURLConnection con =(HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
con.setRequestProperty("Connection", "keep-Alive");
con.setRequestProperty("ENCType", "multipart/form-data");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
con.setRequestProperty("imagefile", sfile);
DataOutputStream dos =new DataOutputStream(con.getOutputStream());
// upload image :
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"imagefile\"; filename=\"" + sfile + "\""+ lineEnd);
dos.writeBytes(lineEnd);
fbyte=fis.available();
buffersize=Math.min(fbyte, maxbuffer);
byte[] buffer=new byte[buffersize];
cbuffer=fis.read(buffer, 0, buffersize);
while(cbuffer>0){
dos.write(buffer,0,buffersize);
fbyte=fis.available();
buffersize=Math.min(fbyte, maxbuffer);
cbuffer=fis.read(buffer,0,buffersize);
}
dos.writeBytes(lineEnd);
String Username = "my username";
String Token = "my token";
//write username :
dos.writeBytes(twoHyphens+ boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"username\";" + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dos.writeBytes(lineEnd + Username + lineEnd);
dos.writeBytes(lineEnd);
//write token :
dos.writeBytes(twoHyphens+ boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"token\";" + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dos.writeBytes(lineEnd + Token + lineEnd);
dos.writeBytes(lineEnd);
if(con.getResponseCode()==200){
fis.close();
dos.flush();
dos.close();
}
}catch(final Exception e){ e.printStackTrace(); }
return "";
}// end doInBackground()
php 边 :
$username = $_POST['username'];
$token = $_POST['token'];
$target_path = "upload/". basename( $_FILES['imagefile']['name']);
move_uploaded_file($_FILES['imagefile']['tmp_name'], $target_path);
echo "username: ".$username." and token: ".$token;
这是自答题
我想发送一些带有要上传的图像的文本,我知道 Apache HttpClient multipart 已被弃用。所以我想用 HTTPUrlConnection 来做到这一点。下面是我的代码,请帮忙。
我得到了答案:我只是重写了我的代码(正确的代码)。希望帮助其他人遇到同样的麻烦。我想发送一张带有用户名和令牌的图片,在 android 和 php 中会像这样:
protected Object doInBackground(Object[] params) {
int fbyte,buffersize,cbuffer;
int maxbuffer=1024*1024;
final String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
BufferedReader reader = null;
try{
String sfile = params[1].toString();
File f=new File(sfile);
FileInputStream fis=new FileInputStream(f);
URL url=new URL(params[0].toString());
HttpURLConnection con =(HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
con.setRequestProperty("Connection", "keep-Alive");
con.setRequestProperty("ENCType", "multipart/form-data");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
con.setRequestProperty("imagefile", sfile);
DataOutputStream dos =new DataOutputStream(con.getOutputStream());
// upload image :
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"imagefile\"; filename=\"" + sfile + "\""+ lineEnd);
dos.writeBytes(lineEnd);
fbyte=fis.available();
buffersize=Math.min(fbyte, maxbuffer);
byte[] buffer=new byte[buffersize];
cbuffer=fis.read(buffer, 0, buffersize);
while(cbuffer>0){
dos.write(buffer,0,buffersize);
fbyte=fis.available();
buffersize=Math.min(fbyte, maxbuffer);
cbuffer=fis.read(buffer,0,buffersize);
}
dos.writeBytes(lineEnd);
String Username = "my username";
String Token = "my token";
//write username :
dos.writeBytes(twoHyphens+ boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"username\";" + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dos.writeBytes(lineEnd + Username + lineEnd);
dos.writeBytes(lineEnd);
//write token :
dos.writeBytes(twoHyphens+ boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"token\";" + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dos.writeBytes(lineEnd + Token + lineEnd);
dos.writeBytes(lineEnd);
if(con.getResponseCode()==200){
fis.close();
dos.flush();
dos.close();
}
}catch(final Exception e){ e.printStackTrace(); }
return "";
}// end doInBackground()
php 边 :
$username = $_POST['username'];
$token = $_POST['token'];
$target_path = "upload/". basename( $_FILES['imagefile']['name']);
move_uploaded_file($_FILES['imagefile']['tmp_name'], $target_path);
echo "username: ".$username." and token: ".$token;