【发布时间】:2014-04-27 07:25:19
【问题描述】:
这是我服务器上的 php 脚本。
<?php
$target_path1 = "/Pictures/"
$target_path1 = $target_path1 . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path1)) {
echo "Success";
} else {
echo "fail";
}
?>
这是我的 Android Java 代码:
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class ImageUpload {
private int serverResponseCode = 0;
private String upLoadServerUri = "http://10.0.2.2/Pictures/UploadToServer.php";
private String imagepath = null;
Context context;
public ImageUpload(Context mcontext) {
context = mcontext;
}
public int uploadFile(String sourceFileUri) {
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);
if (!sourceFile.isFile()) {
// dialog.dismiss();
Log.e("uploadFile", "Source File not exist :" + imagepath);
return 0;
} else {
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(
sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
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);
Toast.makeText(context, fileName, Toast.LENGTH_LONG).show();
conn.setRequestProperty("uploaded_file", /*fileName*/"test.jpg");
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
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);
dos.write(buffer, 0, bytesRead);
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();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
if (serverResponseCode == 200) {
Toast.makeText(
context,
"Image Successfully Shuffled and Sent \n You will be notified once it is solved",
Toast.LENGTH_SHORT).show();
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
Toast.makeText(context, "MalformedURLException",
Toast.LENGTH_SHORT).show();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
Log.e("Upload file to server Exception",
"Exception : " + e.getMessage(), e);
}
return serverResponseCode;
}
}
}
我正在传递 sourceUri 以从我的应用程序上传,类似于 /sdcard/pictures/temp.jpg
请帮助我响应是 HTTP 响应 200,但服务器上没有文件。
使用 XAMP phpmyadmin。
【问题讨论】:
-
这个上传没有问题,但是AFAIK你不能在java servlet中用getParameter("name")检索上传的文件,而是通过getInputStream()得到它,所以你需要找到一个等价的php中的方法
-
您好,感谢您的回复,但我没有使用任何 getParameter。否则你能用正确的代码指导我吗?
-
我也遇到了同样的问题,你找到解决办法了吗? @BVRaman
-
同样的问题..任何解决方案
标签: php android eclipse http xampp