ファイルをリモートサーバにアップロードするためにphpファイルに接続するスクリプトを使用しました。 Android 4.03にアップグレードすると、そのスクリプトは突然サーバーに接続できず、理由がわかりません。 Android 2.3.4以前ではうまくいきます。この問題を持つすべてのヘルプは高く評価されAndroid 4.03でPHP servarへのAndroid(Java)ファイルのアップロードが失敗する
<?php
// Where the file is going to be placed
$target_path = "orders/";
/* Add the original filename to our target path.
Result is "orders/filename.extension" */
$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";
chmod ("orders/".basename($_FILES['uploadedfile']['name']), 0644);
}
else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename($_FILES['uploadedfile']['name']);
echo "target_path: " .$target_path;
}?>
:私たちは、次のPHPファイルを持っているサーバー上
public static String uploadOrderFile(Context c) {
String error = "";
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
FileInputStream fileInputStream = null;
String exsistingFileName = String.format("orders%d.ser", empire.getId());
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String responseFromServer = "";
try {
//------------------ CLIENT REQUEST
// Opening streams.
if(settings.isSaveToExternalStorage()) {
File file = new File(Environment.getExternalStorageDirectory(), String.format("%sorders%s.ser", c.getString(R.string.pathExternalStorage), empire.getId()));
fileInputStream = new FileInputStream(file);
}
else {
fileInputStream = c.openFileInput(exsistingFileName);
}
// open a URL connection to the Servlet
String s = c.getString(R.string.webPageOrders);
URL url = new URL(s);
// 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(String.format("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"%s\"%s", exsistingFileName, 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);
}
catch(FileNotFoundException fnfe) {
error = "Error. Couldn't find the order file internally.";
Log.e(TAG, error, fnfe);
}
catch (MalformedURLException murle) {
error = "Error. Couldn't create correct file URL.";
Log.e(TAG, error, murle);
}
catch(ProtocolException pe) {
error = "Error. Couldn't upload the order file to server.";
Log.e(TAG, error, pe);
}
catch (IOException ioe) {
error = "Error. Couldn't upload the order file to server.";
Log.e(TAG, error, ioe);
}
finally {
try {
// Closing streams and connections.
if(fileInputStream != null) {
fileInputStream.close();
}
if(conn != null) {
conn.disconnect();
}
if(dos != null) {
dos.flush();
dos.close();
}
}
catch(IOException ioe) {
error = "Error. Couldn't disconnect internet stream buffers.";
Log.e(TAG, error, ioe);
}
}
if(!error.equals("")) {
return error;
}
//Reading the server response.
try {
inStream = new DataInputStream (conn.getInputStream());
String str;
while ((str = inStream.readLine()) != null) {
responseFromServer += str;
}
}
catch (IOException ioe){
error = "Error. Couldn't read server response.";
Log.e(TAG, error, ioe);
}
finally {
try {
// Closing streams.
if(inStream != null) {
inStream.close();
}
}
catch(IOException ioe) {
error = "Error. Couldn't disconnect internet stream buffers.";
Log.e(TAG, error, ioe);
}
}
return error;}
。 Android 4.03でそれが失敗するのはなぜですか?それは以前のバージョンとまったく同じですか?
ありがとうございました!
を試してみてください、このリンクを参照してくださいAsyncTaskクラスの
それを修正します。 – mbwasi