大文件不会从我的Android应用程序上传到服务器
私人无效doFileUpload(字符串exsistingFileName){
private void doFileUpload(String exsistingFileName) {
System.out.println("exsistingFileName: " + exsistingFileName);
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String urlString = "SERVER_URL+"/uploadpic2.php";
try {
System.out.println("exsistingFileName: " + exsistingFileName);
FileInputStream fileInputStream = new FileInputStream(new File(
exsistingFileName));
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
/*conn.setReadTimeout(TIMEOUT_CONNECTION);
conn.setConnectTimeout(TIMEOUT_SOCKET);*/
// conn.setChunkedStreamingMode(1024);
conn.setFixedLengthStreamingMode(fileInputStream.available() + 89
+ exsistingFileName.length());
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
System.out.println("exsistingFileName: " + exsistingFileName);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ exsistingFileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
try {
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
System.out.println("MAX BUFFER SIZE IS : " + maxBufferSize);
System.out
.println("BYTES AVAILABLE IS : " + bytesAvailable);
System.out.println("BUFFER SIZE IS : " + bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
} catch (Exception e) {
System.out.println("OUT OF MEMORY ERROR");
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
try {
inStream = new DataInputStream(conn.getInputStream());
String str;
while ((str = inStream.readLine()) != null) {
Log.e("Error: ", "Server Response" + str);
}
inStream.close();
// Toast.makeText(this, "File Uploaded Successfully",
// Toast.LENGTH_LONG).show();
} catch (IOException ioex) {
Log.e("Error: ", "error: " + ioex.getMessage(), ioex);
}
try {
fileInputStream.close();
dos.flush();
dos.close();
dos = null;
} catch (Exception e) {
System.out.println("STREAM CLOSED ERROR");
}
} catch (MalformedURLException ex) {
Log.e("Error:", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe) {
Log.e("Error:", "error: " + ioe.getMessage(), ioe);
}
}
我有这个code上传外商投资企业服务器,但这个我不是能上传文件的大小超过8MB;它提供了例外,即FileNotFoundException异常SERVER_URL +/ uploadpic2.php找不到。请任何机构帮我解决这个错误。
谢谢你。
i have this code to upload fie to server but with this i m not able to upload a file with size more than 8MB;it gives the exception as FileNotFoundException that SERVER_URL+"/uploadpic2.php" not found. Please any body help me to fix this error. Thanks.
根据这个网页你需要在你的PHP配置文件更改设置。
According to this web page you need to change a setting in your php config file.
此限制强制执行有
post_max_size = 16M
upload_max_filesize = 16M
另外我觉得行
字符串urlString =SERVER_URL +/ uploadpic2.php;
应该读
字符串urlString = SERVER_URL +/uploadpic2.php;
更新
我发现another 的问题类似于你。这看起来像PhoneGap的一个问题,根据这个问题。
I found another question similar to yours. This looks like an issue with phoneGap according to that question.