我正在开发一个Android应用程序。在此应用中,需要使用其API将图像上传到imageShack网站。
这里的“ sourceFileUri”是来自设备sdcard的图像文件路径。它显示Outh或dev Key无效。.请任何人帮助我找出错误。高级谢谢。
private void goForUpload(final String sourceFileUri)
{
if (!SharedPreferencesHelper.isOnline(con))
{
return;
}
pDialog = ProgressDialog.show(this, "Please wait...", "Loading...",false, false);
final Thread d = new Thread(new Runnable()
{
@Override
public void run() {
String upLoadServerUri = " http://www.imageshack.us/upload_api.php";
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
Log.w("file name are...", "" + sourceFile);
if (!sourceFile.isFile()) {
Log.e("uploadFile", "Source File Does not exist");
}
try { // open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a
// HTTP
// connection
// to
// the
// URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
//conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
// ///////////////////////////////////////////////////////////////////
//for image
dos.writeBytes("Content-Disposition: form-data; name=\"fileupload\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"key\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes("289DGHSTbbfb01094c0017d23e96fe1edecda161");
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
InputStream servere = conn.getInputStream();
String uyy = HttpRequest.GetText(servere);
System.out.print(uyy);
Log.w("uyy", "" + uyy);
Log.i("uploadFile", "HTTP Response is : "+ serverResponseMessage + ": " + serverResponseCode);
if (serverResponseCode == 200)
{
runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(MainActivity.this,"File Upload Complete.",Toast.LENGTH_SHORT).show();
}
});
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
// Log.w("url", ""+url);
// final String url =
// "http://www.imageshack.us/upload_api.php?fileupload=http://www.libpng.org/pub/png/img_png/pnglogo-blk.jpg&url="+fos+"&optsize=100x100&rembar=yes&key=DHKNPRWYb185051e16c4545d8f26828d6fd3886c";
// final String url =
// "https://api.mobypicture.com/2.0/upload.json";//key=Az7IN9Qaxu3eZeK5/media=http://www.libpng.org/pub/png/img_png/pnglogo-blk.jpg/message=wasir";
//
// final String result = HttpRequest.GetText(HttpRequest
// .getInputStreamForGetRequest(url));
//
// try {
// if (parser.connect(con, result)) {
// Log.d("What is result :", result);
// }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* update the GUI
*/
runOnUiThread(new Runnable() {
@Override
public void run() {
if (pDialog != null) {
pDialog.cancel();
}
}
});
}
});
d.start();
}
最佳答案
首先是您拥有表单数据部分的顺序:
dos.writeBytes("Content-Disposition: form-data; name=\"key\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes("xxxxxxxxxxxxxxxxxx");
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
//for image
dos.writeBytes("Content-Disposition: form-data; name=\"fileupload\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
dos.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
dos.writeBytes(lineEnd);
//dos.writeBytes(twoHyphens + boundary + lineEnd);
第二个问题(尝试时)是图像类型无效,因此如上所述添加了内容类型。而且,在写入文件内容之前不需要边界。