大于 2 MB 的文件不会上传到服务器
Files which are greater than 2 MB in size are not uploading on server
在我的应用程序中,我需要实现上传图像、文本文件、zip 文件夹和 Pdf 文件。 img\txt\zip\pdf 一切都很好,但是当我选择一个大于 2 mb 的文件时,它不会上传。以下是我的代码。
public int uploadFile(String sourceFileUri)
{
try
{
StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
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())
{
title=filename+"";
generateNotification(title, "Upload Failed..File Not Found");
}
else
{
FileInputStream fileInputStream = new FileInputStream(sourceFile);
String upLoadServerUri = "http://xx.xx.xx.xxx:xx/phpwebservice/upload.php";
URL url = new URL(upLoadServerUri);
// 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();
if(serverResponseCode == 200)
{
status = true;
}
else
{
title=filename+"";
generateNotification(title, "File Not Uploaded");
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
}
}
catch (final MalformedURLException ext)
{
title=filename+"";
generateNotification(title, "File Not Uploaded");
}
catch (final Exception etx)
{
title=filename+"";
generateNotification(title, "File Not Uploaded");
}
//dialog.dismiss();
return serverResponseCode;
}
请提供解决方案:
- 如何使用此方法上传大于 2Mb 的文件?
我的PHP端网络服务代码是:
<?php
$file_path = "a/";
$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";
}
?>
我通过更新 php.ini 得到了与尺寸相关的问题的答案,它对我有用
我在 php.ini 的两个地方
进行了更改
upload_max_filesize = 4M
post_max_size = 0
更改此设置后,我的 pdf 文件也得到保存
在我的应用程序中,我需要实现上传图像、文本文件、zip 文件夹和 Pdf 文件。 img\txt\zip\pdf 一切都很好,但是当我选择一个大于 2 mb 的文件时,它不会上传。以下是我的代码。
public int uploadFile(String sourceFileUri)
{
try
{
StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
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())
{
title=filename+"";
generateNotification(title, "Upload Failed..File Not Found");
}
else
{
FileInputStream fileInputStream = new FileInputStream(sourceFile);
String upLoadServerUri = "http://xx.xx.xx.xxx:xx/phpwebservice/upload.php";
URL url = new URL(upLoadServerUri);
// 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();
if(serverResponseCode == 200)
{
status = true;
}
else
{
title=filename+"";
generateNotification(title, "File Not Uploaded");
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
}
}
catch (final MalformedURLException ext)
{
title=filename+"";
generateNotification(title, "File Not Uploaded");
}
catch (final Exception etx)
{
title=filename+"";
generateNotification(title, "File Not Uploaded");
}
//dialog.dismiss();
return serverResponseCode;
}
请提供解决方案:
- 如何使用此方法上传大于 2Mb 的文件?
我的PHP端网络服务代码是:
<?php
$file_path = "a/";
$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";
}
?>
我通过更新 php.ini 得到了与尺寸相关的问题的答案,它对我有用 我在 php.ini 的两个地方
进行了更改upload_max_filesize = 4M
post_max_size = 0
更改此设置后,我的 pdf 文件也得到保存