我想将一个文本文件从java类发送到php并将其存储在服务器上。但是文件永远不会到达那里
这是我的Java代码:
public class JavaUrlConnectionReader
{
public static void main(String[] args) throws Exception {
HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL("http://URL/upload.php").openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
File myFile = new File ("/path_to_file/test.txt");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
OutputStream os = httpUrlConnection.getOutputStream();
BufferedInputStream bis= new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
System.out.println("Sending the file of size:"+ mybytearray.length + " bytes");
os.write(mybytearray,0,mybytearray.length);
System.out.println("File sent.");
BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
String s = null;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
os.flush();
bis.close();
os.close();
fis.close();
in.close();
}
}
这是PHP脚本
<?php
$target_path = "../output/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
?>
似乎文本文件永远不会到达那里。我认为java代码应该没问题。我在 php 文件中做错了什么。任何帮助 php 文件有什么问题?
这是我用来上传文件的方式。也许它会帮助你。
String postReceiverUrl = "http://URL/upload.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(postReceiverUrl);
//Create File
File file = new File("/path_to_file/test.txt");
FileBody fileBody = new FileBody(file);
//Set up HTTP post
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("MyTxtFile", fileBody);
httpPost.setEntity(reqEntity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
System.out.println(responseStr);
System.out.println(response.getStatusLine());
}
上传.php文件
<?php
$target_path = "../output/";
$target_path = $target_path . basename( $_FILES['MyTxtFile']['name']);
if(move_uploaded_file($_FILES['MyTxtFile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['MyTxtFile']['name'])." has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
?>
似乎您只是准备了 POST 参数,并没有最终发送 HTTP 请求。
我更喜欢使用 Apache httpcomponents-client 库,这里非常简单:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
for (Entry<String, String> p : postArgs.entrySet()) {
params.add(new BasicNameValuePair(p.getKey(), p.getValue()));
}
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
// Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
return instream;
}