小编use*_*019的帖子

图像上传CURL命令到PHP Curl

我是PHP CURL的新手,并试图调用API来进行图像上传(一次多个文件).API文档在CURL中给出了以下示例.我已经测试过,它正在运行.

curl -H "Authorization: Bearer 123456789" -i -X POST -F "whitespace=1" \
 -F "imageData[]=@/path/to/images/milk.jpg" \
 -F "imageData[]=@/path/to/images/peas.jpg" \
 https://mytestapi.com/1.0/uploadimages
Run Code Online (Sandbox Code Playgroud)

现在我需要将其转换为PHP Curl.出于某种原因,我总是得到错误"imageData"param无效.有人可以请帮助

$token = '123456789';
$imgUrl = 'https://mytestapi.com/1.0/uploadimages';


$data_to_post = array();
$data_to_post['whitespace'] = '1';
$data_to_post['imageData[]'] = '@/path/to/images/milk.jpg';
$data_to_post['imageData[]'] = '@/path/to/images/peas.jpg';


$curl = curl_init($imgUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$token]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);
$data = json_decode(curl_exec($curl));
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

var_dump($data);
Run Code Online (Sandbox Code Playgroud)

php curl libcurl image-upload

8
推荐指数
1
解决办法
1224
查看次数

Java Servlet中的ExecutorService

我需要在java servlet中同时执行一些任务(主要是使用请求参数和读取数据调用多个外部URL)并在几秒钟内向用户发送响应.我正在尝试使用ExecutorService来实现相同的目的.我需要在doGet方法中的每个用户请求中创建四个FutureTasks.每个任务运行大约5-10秒,对用户的总响应时间约为15秒.

在Java servlet中使用ExecutorService时,能否建议以下哪种设计更好?

1)(为每个请求创建newFixedThreadPool并尽快关闭它)

public class MyTestServlet extends HttpServlet
{

    ExecutorService myThreadPool = null;

    public void init()
    {
          super.init();

    }
    protected void doGet(HttpServletRequest request,HttpServletResponse response)
    {

        myThreadPool = Executors.newFixedThreadPool(4);
        taskOne   = myThreadPool.submit();
        taskTwo   = myThreadPool.submit();        
        taskThree = myThreadPool.submit();
        taskFour  = myThreadPool.submit();

        ...
        ...

        taskOne.get();
        taskTwo.get();
        taskThree.get();
        taskFour.get();

        ...

        myThreadPool.shutdown();


    }

     public void destroy()
     {

         super.destroy();
     }

}
Run Code Online (Sandbox Code Playgroud)

2)(在Servlet Init期间创建newFixedThreadPool并在servlet destroy上关闭它)

public class MyTestServlet extends HttpServlet
{

    ExecutorService myThreadPool = null;

    public void init()
    {
      super.init();
          //What should be the …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading servlets java-ee

5
推荐指数
1
解决办法
5262
查看次数