gpt4 book ai didi

php - 如何将 pdf/word/文本文件发送到服务器

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:28:09 25 4
gpt4 key购买 nike

这是我的代码。但是我的服务器没有得到正确的数据我正在尝试通过 httpclient 发送文件我还读到 http 客户端在 6.0 之后被弃用那么我应该使用什么方法呢并且可以发布他们的工作/运行代码在那里我可以通过我的 android 手机将我的 word/pdf/文本文件发送到 php 服务器

package com.example.sendfiletoserver;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;


public class SendFileToServer extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_file_to_server);
new postFileAsync().execute();
}

private void uploadFile() throws ClientProtocolException, IOException {
String file=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Test.pdf";
Log.d("Mayur", ""+file);

HttpClient httpclient=new DefaultHttpClient();
HttpPost post=new HttpPost("<My_address>"); //My server address
File f=new File(file);
FileBody filebody=new FileBody(f);
MultipartEntity mentity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mentity.addPart("file", filebody);
post.setEntity(mentity);
HttpResponse responce=httpclient.execute(post);
HttpEntity entity=responce.getEntity();

}

public class postFileAsync extends AsyncTask<Void, Void, Void> {
// ProgressDialog pd;

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
// pd = new ProgressDialog(SendFileToServer.this);
// pd.setCancelable(false);
// pd.setMessage("Uploading File");
// pd.show();
}

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub

try {
uploadFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
// pd.dismiss();
}

}
}

这是我的php代码

<?php
// if text data was posted
if ($_POST) {
print_r($_POST);
}

// if a file was posted
else if ($_FILES) {
$file = $_FILES['file'];
$fileContents = file_get_contents($file["tmp_name"]);
print_r($fileContents);
}
?>

我对php一窍不通所以如果有的话可以给我 php 代码来帮助我。我什至不知道我的代码 ID 是否正确

提前谢谢你

最佳答案

你可以用这个,这段代码对我有用,

HttpURLConnection connection = null;
HttpURLConnection.setFollowRedirects(false);
connection.setRequestMethod("POST");
String boundary = "---------------------------boundary";
String tail = "\r\n--" + boundary + "--\r\n";
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.setDoOutput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");

String metadataPart = "--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n"
+ "" + "\r\n";

String fileHeader1 = "--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"uploaded_file\"; filename=\""
+ URLEncoder.encode(fileName, "UTF-8") + "\"\r\n";
// + "Content-Type: application/octet-stream\r\n"
// + "Content-Transfer-Encoding: binary\r\n";

long fileLength = file.length() + tail.length();
String fileHeader2 = "Content-length: " + fileLength + "\r\n";
String fileHeader = fileHeader1 + fileHeader2 + "\r\n";
String stringData = metadataPart + fileHeader;
long requestLength = stringData.length() + fileLength;
connection.setRequestProperty("Content-length", "" + requestLength);
connection.setRequestProperty("Connection", "close");
connection.setFixedLengthStreamingMode((int) requestLength);
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(stringData);
out.flush();
int progress = 0;
int bytesRead = 0;
int totbyts = 0;
byte buf[] = new byte[1024];
BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file));
while ((bytesRead = bufInput.read(buf)) > 0) {
// write output
out.write(buf, 0, bytesRead);
out.flush();
totbyts += bytesRead;
// update progress bar
progress = (int) (totbyts * 100 / file.length());
publishProgress(progress);
if (isCancelled()) {
connection.disconnect();
break;
}
}

并且您的 PHP 代码也可以正常工作。希望这对你有用。我知道现在回答已经很晚了

关于php - 如何将 pdf/word/文本文件发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33230671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com