在我的应用程序中,我需要实现上传图像,文本文件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;
}
请提供以下解决方案:
我的PHP Side Web服务代码是:
<?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";
}
?>
答案 0 :(得分:0)
通过更新php.ini,我得到了与大小相关的问题的答案 我在两个地方更改了我的php.ini
upload_max_filesize = 4M
post_max_size = 0
更改此设置后,我的pdf文件也会被保存