使用 POST 将图像文件上传到 HTTP 服务器
我正在构建一个应用程序,首先需要在其上注册...注册表单外观-> 这个
它有一些必需的&一些可选字段和用户图像也是可选的。我正在服务器上发送此表单数据还上传用户图像。我遵循这个< /a> 上传用户图片&我上传用户图像的代码是 ->
public String uploadingMyImageOnServer() {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
/** path of my image */
String pathToMyImageFile;
if(myImagePath!=null){
pathToMyImageFile = myImagePath; // "myImagePath" is the path which i get from gallery or from camera where image resides!
//Log.i("MyImagePath", pathToMyImageFile);
}else{
// i want to store this image when user does not provide his/her profile image
pathToMyImageFile="/"+res.getString(R.drawable.user_logo).trim();
Log.i("## Default pathToMyImageFile...##", "/"+res.getString(R.drawable.user_logo).trim()); // give this string -> /res/drawable/user_image.png
}
// [-----------------------------------------------------------
/** my url-> To Uplopad user`s Image on php server */
//String urlServer = "http://xxxxxxxx/yyyyy/upload_image_and.php";//Live
String urlServer = "http://192.168.200.111/ManU/upload_image_and.php";//Local
// -----------------------------------------------------------]
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToMyImageFile) );
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"photo\";filename=\"" + uniqid +".png"+"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// ...Responses from the server (code and message)...
// ResponseCache serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.i("ImgServerrespons", serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
return "successfrlly";
}
catch (Exception ex)
{
//Exception handling
}
return "failed";
}
当用户提供他的个人资料图片像这样时,效果很好,但如果他/她不提供个人资料那么它不会上传我从我的资源中提供的默认图像
(this->res/drawable/user_image.png)
如何在 php 服务器上上传此默认图像?
I am building one app which first require to register on it... registration form look->
this
It have some required & some optional fields and user image which is also optional. i am sending this form data on server & uploading user image as well. I follow this to upload user image & my code to upload user image is ->
public String uploadingMyImageOnServer() {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
/** path of my image */
String pathToMyImageFile;
if(myImagePath!=null){
pathToMyImageFile = myImagePath; // "myImagePath" is the path which i get from gallery or from camera where image resides!
//Log.i("MyImagePath", pathToMyImageFile);
}else{
// i want to store this image when user does not provide his/her profile image
pathToMyImageFile="/"+res.getString(R.drawable.user_logo).trim();
Log.i("## Default pathToMyImageFile...##", "/"+res.getString(R.drawable.user_logo).trim()); // give this string -> /res/drawable/user_image.png
}
// [-----------------------------------------------------------
/** my url-> To Uplopad user`s Image on php server */
//String urlServer = "http://xxxxxxxx/yyyyy/upload_image_and.php";//Live
String urlServer = "http://192.168.200.111/ManU/upload_image_and.php";//Local
// -----------------------------------------------------------]
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToMyImageFile) );
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"photo\";filename=\"" + uniqid +".png"+"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// ...Responses from the server (code and message)...
// ResponseCache serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.i("ImgServerrespons", serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
return "successfrlly";
}
catch (Exception ex)
{
//Exception handling
}
return "failed";
}
when user provide his profile image like this then it works well but if he/she does not provide profile image then it does not upload the default image,
which i provide from my resources(this->res/drawable/user_image.png)
How to upload this default image on php Server ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗨,我以前也遇到过这个问题,并找到了一个非常酷的解决方案。只需使用卷曲过程即可。
Hi ive had this problem before too and found a really cool solution. Just use a curl Process.
请尝试以下代码段,请忽略经纬度编码。它与我的项目配合得很好。
kindly try following code segment, Please ignore lat-lon coding. Its work fine with my project.