我需要在lampp ht-docs
目录中的共享文件夹上传文件。
该文件位于内部存储器中。我也有这个文件的路径。
我尝试了一些解决方案,但它没有将文件移动到lampp ht-docs
内的共享文件夹。
我有URL
这样http://192.168.1...../OrderFiles/
。 OrderFiles是lampp htdocs
目录中的共享文件夹。我想在file
的文件路径中上传internal storage
。
以下是我的尝试。
public void uploadLocal() {
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
File fileName = new File(DBController.lastXmlPathLocal);
Log.w("getAbsolutePath", fileName.getAbsolutePath());
Log.w("getPath", fileName.getPath());
String existingFileName = fileName.getAbsolutePath();
Log.w("existingFileName", existingFileName);
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String responseFromServer = "";
String urlString = "http://192.168.1......./OrderFiles/";
Log.w("urlString", urlString);
try {
//------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(fileName.getPath()));
// 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=\"" + existingFileName + "\"" + 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)
Log.v("info", ".size." + bytesRead);
for (int n1 = 0; n1 < bytesRead; n1++) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.v("info", "File is written");
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
Log.v("info", "error: " + ex.getMessage(), ex);
} catch (IOException ioe) {
Log.v("info", "error: " + ioe.getMessage(), ioe);
}
/*//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream(conn.getInputStream());
String str;
while ((str = inStream.readLine()) != null) {
Log.v("info", "Server Response " + str);
}
inStream.close();
} catch (IOException ioex) {
Log.v("info", "error: " + ioex.getMessage(), ioex);
}*/
}
以上代码在File is written
中显示logcat
,但在该文件夹中未显示任何文件。
他们的任何其他解决方案是否可以整合这个?
非常感谢任何帮助。
答案 0 :(得分:0)
首先,您需要一个PHP脚本来上传文件。之后,php脚本应该包含将上传的文件移动到您想要的文件夹的代码。这就是它的方式。这就是你应该怎么做的。