我正在构建一个首先需要在其上注册的应用程序...注册表单look-> this
它有一些必要的&一些可选字段和用户图像,也是可选的。我将此表格数据发送到服务器&也可以上传用户图片。我按照this上传用户图片&我上传用户图片的代码是 - >
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";
}
当用户提供他的个人资料图片like this时,它运作良好,但如果他/她不提供个人资料图片,那么它不会上传默认图片,
我从我的资源中提供(this-> res / drawable / user_image.png)
如何在php服务器上传这个默认图像?
答案 0 :(得分:0)
嗨,我之前也遇到过这个问题,并找到了一个非常酷的解决方案。只需使用卷曲过程。
String[] cmd="curl --form upload=@record.jpg -m 20 http://api.face.com/faces/detect.json".split(" ");
ProcessBuilder pb=new ProcessBuilder(cmd);
p=pb.start();
int cur=-1;
InputStream in2=p.getInputStream();
ByteArrayOutputStream bout=new ByteArrayOutputStream();
cur=-1;
while ((cur=in2.read())!=-1) {
bout.write(cur);
}
String text=bout.toString();
System.out.println(text);
答案 1 :(得分:0)
请尝试以下代码段,请忽略lat-lon编码。它与我的项目一起工作得很好。
private void postImageToServer(String curURL) throws Exception
{
// Open up a http connection with the Web server for both send and receive operations
midlet.get_frmLog().append("HttpConnection Enter");
httpConnection = (HttpConnection)Connector.open(URL, Connector.READ_WRITE);
// Set the request method to POST
httpConnection.setRequestMethod(HttpConnection.POST);
// Set the request headers
httpConnection.setRequestProperty(ConstantCodes.ACTION_MODE_PARAMETER,action);
httpConnection.setRequestProperty(ConstantCodes.USER_NAME_REQUEST_PARAMETER,userName);
//httpConnection.setRequestProperty("lat","22.955804");
//httpConnection.setRequestProperty("lon","72.685876");
//httpConnection.setRequestProperty("lat",LatLonFetcherThread.getLatitude());
//httpConnection.setRequestProperty("lon",LatLonFetcherThread.getLongitude());
/*lat = "23.0172";
lon = "72.3416";*/
if ( midlet.get_choiceGroupGPS().getSelectedIndex() == 0 )
{
lat = LatLonFetcherThread.getLatitude();
lon = LatLonFetcherThread.getLongitude();
}
else if ( midlet.get_choiceGroupGPS().getSelectedIndex() != 0 ) // Added By KALPEN
{
if ( midlet.state == mREPORTER.STATE_READING )
{
double xLat,xLon;
try
{
GpsBt.instance().start();
//while (true)
{
gpsBt = GpsBt.instance();
if ( gpsBt.isConnected() )
{
location = gpsBt.getLocation();
//lat = location.utc;
//lon = location.utc;
lat = (location.latitude + location.northHemi);
lon = (location.longitude + location.eastHemi);
lat = lat.substring(0,lat.length()-1);
lon = lon.substring(0,lon.length()-1);
xLat = Double.parseDouble(lat) / (double)100.00;
xLon = Double.parseDouble(lon) / (double)100.00;
lat = xLat + "";
lon = xLon + "";
lat = lat.substring(0,7);
lon = lon.substring(0,7);
//lat = "23.0172";
//lon = "72.3416";
}
Thread.sleep(100);
}
}
catch ( Exception e ) { lat = "23.0172"; lon = "72.3416"; }
}
}
httpConnection.setRequestProperty("lat",lat); // Modifed by KALPEN
httpConnection.setRequestProperty("lon",lon); // Modifed by KALPEN
//String latlongStr = "Latitude: " + LatLonFetcherThread.getLatitude()
// + "\nLongitude: " + LatLonFetcherThread.getLongitude();
/*String latlongStr = "lat: 22.955804" + "lon: 72.685876";*/
//#style screenAlert
/*Alert latlonAlert = new Alert("LatLon alert" );
latlonAlert.setString(latlongStr);
mREPORTER.display.setCurrent(latlonAlert);*/
if(eventName == null || eventName.equals(""))
eventName = "default";
// all the headers are sending now and connection channel is establising
httpConnection.setRequestProperty(ConstantCodes.EVENT_NAME_REQUEST_PARAMETER, eventName);
httpConnection.setRequestProperty(ConstantCodes.CAMERAID_REQUEST_PARAMETER, cameraID);
httpConnection.setRequestProperty(ConstantCodes.COMPRESSION_MODE_REQUEST_PARAMETER, compressionMode);
midlet.get_frmLog().append("Write on server-->"+imageBytes.length);
DataOutputStream dos = httpConnection.openDataOutputStream();
dos.write(imageBytes);
midlet.threadFlag=true;
}