doudi4014 2015-01-21 11:14
浏览 98

如何使用httpClient将文件上载到特定文件夹

I am trying to upload a zip file to FTP server using Apache HttpClient.

My java code is like this:

public void doFileUpload(String Localfile, String myurl) {
HttpClientBuilder builder = HttpClientBuilder.create();
HttpClient httpClient = builder.build();

HttpPost httpPost = new HttpPost(myurl);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.addBinaryBody("file", new File(Localfile));

final HttpEntity entity = entityBuilder.build();
System.out.println(entity.getContentLength());

httpPost.setEntity(entity);
        try {
            HttpResponse response = httpClient.execute(httpPost);
            System.out.println(response.toString());
            System.out.println("Success");
        } catch (IOException  ex) {
            System.out.println("Failed"+ex);
            Logger.getLogger(FileUpload.class.getName()).log(Level.SEVERE, null, ex);
        }

In my php File :

if (!empty($_FILES["file"]["name"])) {
        $dat = time();
    $file_name=$_FILES["file"]["name"];
    $temp_name=$_FILES["file"]["tmp_name"];
        $target_path = $file_name."-".$dat;

if (is_uploaded_file($temp_name)) {
  echo "File ". $target_path ." uploaded successfully.
";
  move_uploaded_file ($temp_name, $target_path);
} else {
  echo "Possible file upload attack: ";
  echo "filename '". $temp_name . "'.";
  print_r($_FILES);
}
}

This works fine if I upload a file to the folder where my php file exists. But In my condition, user will upload their files to their own folder. Example: User A will upload a file to myftp/upload/A/ or user B will upload a file to myftp/upload/B/ The target folder is defined from the user end. I can create folder for all the users manually and place a php file in every folder. But how can I do this with a single php file ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行
    • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
    • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: