Java 将图片从Android上传到PHP服务器
我正在尝试从我的android设备上传文件到php服务器。有人问了同样的问题,但他用的是不同的方法。我的Android端代码工作正常,并没有显示错误消息,但服务器并没有收到任何文件。这是我的示例代码,我在网上找到的Java 将图片从Android上传到PHP服务器,java,android,http,file-upload,Java,Android,Http,File Upload,我正在尝试从我的android设备上传文件到php服务器。有人问了同样的问题,但他用的是不同的方法。我的Android端代码工作正常,并没有显示错误消息,但服务器并没有收到任何文件。这是我的示例代码,我在网上找到的 import java.io.FileInputStream; import android.app.Activity; import android.os.Bundle; import java.io.DataInputStream; import java.io.DataOutp
import java.io.FileInputStream;
import android.app.Activity;
import android.os.Bundle;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
public class uploadfile extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
doFileUpload();
}
private void doFileUpload(){
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String exsistingFileName = "/sdcard/def.jpg";
// Is this the place are you doing something wrong.
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String responseFromServer = "";
String urlString = "http://192.168.1.6/index.php";
try
{
//------------------ CLIENT REQUEST
Log.e("MediaPlayer","Inside second Method");
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ exsistingFileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e("MediaPlayer","Headers are written");
// 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);
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);
// close streams
Log.e("MediaPlayer","File is written");
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
Log.e("MediaPlayer","Server Response"+str);
}
inStream.close();
}
catch (IOException ioex){
Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
}
}
}
我的php服务器端代码如下
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
?>
服务器似乎没有响应客户端。尝试通过Android应用程序使用ftp连接进行上传,如果可以,请检查Apache在接受连接和可写目录方面的配置。当我遇到类似的问题时,结果是我的目录没有写入权限
错误是来自Java还是来自Apache?请按以下方式更改代码以获得正确的转义序列: 替换
String lineEnd = "rn";
与
我正在连接,但ftp客户端无法连接到服务器。我不知道如何运行服务器。我是否需要运行apache并在浏览器中打开服务器url?还是我需要做点别的?错误消息在eclipse日志中。如果您试图通过FTP传输数据,那么Apache与此无关,您需要运行FTP服务器。在这种情况下,您不需要PHP文件来“捕获”正在上载的内容。另一方面,如果您试图使用PHP上传,那么应该使用“_POST”将文件名从Java传递到PHP。你给我们的代码是正确的,但是它似乎不知道要上传的文件名。希望这能有所帮助。但是有什么问题呢?它不知道要上传的文件名。当我从上面提供的android代码上传文件时,我收到了一条消息“文件已写入”。但在服务器端,我收到错误消息“上传文件时出错,请重试!”。这可能意味着您的目录不可写。您应该对目录进行chmod以允许WritingKey。。。非常感谢您的回复。让我用另一个目录再试一次。更改的标签-这似乎与PHP无关-您应该能够使用传统浏览器通过一个简单的测试页面来检查这一点,但我认为这是一个PHP问题,因为服务器代码是PHP。我试过这段代码,从中可以看出POST请求中没有发送任何文件。我使用var_dump($_文件)进行了测试;这里有一个有用的例子。试试看。“这会对你有帮助的。”傻编码器,你做了什么?
String lineEnd = "rn";
String lineEnd = "\r\n";