我希望用户将pdf上传到服务器并将其下载到其他用户。我想将上传文件的URL保存到数据库,然后拾取该URL以显示其他用户。我正在学习android,所以我没有太多的想法。 这是我的代码,我曾尝试使用multipart: -
i
}
答案 0 :(得分:2)
完整的工作示例,针对超过100 Mb的文件进行了测试,将your_package
替换为您的包名称。
<强>的AndroidManifest.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your_package" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Browse"
android:id="@+id/browse"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Upload"
android:id="@+id/upload"/>
</LinearLayout>
活动主要活动
package your_package;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
Uri path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button browse = (Button) findViewById(R.id.browse);
Button upload = (Button) findViewById(R.id.upload);
browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select PDF"), 1);
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
(new Upload(MainActivity.this, path)).execute();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
path = result.getData();
}
}
}
}
class Upload extends AsyncTask<Void, Void, Void> {
private ProgressDialog pd;
private Context c;
private Uri path;
public Upload(Context c, Uri path) {
this.c = c;
this.path = path;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(c, "Uploading", "Please Wait");
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pd.dismiss();
}
@Override
protected Void doInBackground(Void... params) {
String url_path = "http://192.168.43.50/projectpri/upload.php";
HttpURLConnection conn = null;
int maxBufferSize = 1024;
try {
URL url = new URL(url_path);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setChunkedStreamingMode(1024);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data");
OutputStream outputStream = conn.getOutputStream();
InputStream inputStream = c.getContentResolver().openInputStream(path);
int bytesAvailable = inputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = inputStream.read(buffer, 0, bufferSize)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
Log.i("result", line);
}
reader.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
}
服务器端PHP
$file_name = date("U").".pdf"; //name of your file
$server_path = "/path/to/file/folder/"; //server path to folder
$web_path = "http://192.168.43.50/projectpri/file/"; //web path to folder
$file = $server_path.$file_name;
file_put_contents($file,"");
$fp = fopen("php://input", 'r');
while ($buffer = fread($fp, 8192)) {
file_put_contents($file,$buffer,FILE_APPEND | LOCK_EX);
}
echo $web_path.$file_name;
答案 1 :(得分:1)
首先,您不应该使用HttpClient,MultipartEntitiy,因为它们已弃用而根本不使用不推荐 http://developer.android.com/。
为方便起见,请查看以下链接:
常识:
Android deprecated apache module (HttpClient, HttpResponse, etc.)
http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
适用于发布到服务器的有用链接:
Upload multiple image file using HttpURLConnection
要将数据发布到服务器,您也可以轻松使用库 像凌空一样,改造。等
您必须对其进行一些实验,以便在各种Android版本和有效方式下安全地保护您的代码。
谢谢