我有一个Android应用程序,用户可以在其中生成SQLite表的转储文件,zip文件并上传。
转储和压缩工作。我能够生成zip文件,将其解压缩并获得原始的转储文件。
但是,上传无法正常工作。
这是我的代码。我有一个启动Runnable的函数,该函数先启动转储文件,然后启动zip,最后到达uploadFile函数:
new Thread(new Runnable() {
public void run() {
createDumpFile();
}
}).start();
public int uploadFile(String sourceFileUri) {
Log.e(TAG, "uploading file");
Log.e(TAG, "sourceFileUri = " + 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(TAG, "Source File not exist :"
+ sourceFileUri);
return 0;
}
else {
try {
Log.e(TAG, "file exists");
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL("http://www.myurl.com/subfolder/uploadToServer.php");
// 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);
conn.setRequestProperty("uploaded_file", fileName);
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);
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.e(TAG, "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200){
Log.e(TAG, "Upload success!");
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
Log.e(TAG, "result is = " + result.toString());
}
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
dialog.dismiss();
ex.printStackTrace();
Log.e(TAG, "error: " + ex.getMessage(), ex);
} catch (Exception e) {
dialog.dismiss();
e.printStackTrace();
Log.e(TAG, "Exception : " + e.getMessage(), e);
}
dialog.dismiss();
return serverResponseCode;
} // End else block
}
这是接收文件的PHP代码:
<?php
$file_path = "database/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path) ){
echo "success";
} else{
echo "fail";
}
?>
返回的响应代码为200,大多数在线指南认为该指标足以说明文件已上传。但是,当我使用FileZilla检查服务器中的“数据库”文件夹时,发现文件没有上传。
这促使我检查PHP的echo语句。很快,我看到echo语句为“ fail”。
我尝试查找其他指南,但是它们大致相同,除了传递给uploadFile
函数的String和对mainUIThread的少量更新之外。
此代码在哪里出错?
答案 0 :(得分:0)
代码有效。只要确保您对文件所在的文件夹具有适当的读写权限即可。否则,您将无法在其中进行写入。
如果您拥有对服务器的ssh访问权限,请执行以下一种方法:
sudo chmod 777 /path/to/your/folder/here