Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Servlets 如何在iphone浏览器中强制从服务器下载文件_Servlets - Fatal编程技术网

Servlets 如何在iphone浏览器中强制从服务器下载文件

Servlets 如何在iphone浏览器中强制从服务器下载文件,servlets,Servlets,以上代码适用于所有浏览器中的android设备。但它在iphone中不起作用 点击下载按钮后,图像直接显示。不问任何问题 我想将图像存储到iphone中的gallary。 有谁能帮我一下吗?iphone有可能吗 String filePath = "http://images.all-free-download.com/images /graphiclarge/baby_child_girl_215819.jpg"; The above String is m

以上代码适用于所有浏览器中的android设备。但它在iphone中不起作用

点击下载按钮后,图像直接显示。不问任何问题

我想将图像存储到iphone中的gallary。
有谁能帮我一下吗?iphone有可能吗

String filePath = "http://images.all-free-download.com/images
        /graphiclarge/baby_child_girl_215819.jpg";

         The above String is my download path.

          URL url = new URL(filePath);
        HttpURLConnection httpConn = (HttpURLConnection)   
        url.openConnection();
        InputStream inStream = httpConn.getInputStream();

        // if you want to use a relative path to context root:
        @SuppressWarnings("unused")
        String relativePath = getServletContext().getRealPath("");

        // obtains ServletContext
        ServletContext context = getServletContext();

        // gets MIME type of the file
        String mimeType = context.getMimeType(filePath);
        if (mimeType == null) {        
            // set to binary type if MIME mapping not found
            mimeType = "application/octet-stream";
        }

        // modifies response
        response.setContentType(mimeType);
        response.setContentLength((int) httpConn.getContentLength());

        String fileName = filePath.substring(filePath.lastIndexOf("/") +
         1,
                filePath.length());

        // forces download
        //String headerKey = "Content-Disposition";
        //String headerValue = String.format("attachment; 
         filename=\"%s\"", fileName);
        response.setHeader("Content-Transfer-Encoding", "binary");
        response.setHeader("Content-Disposition","attachment; 
          filename=\""+ fileName);
        //response.setHeader(headerKey, headerValue);

        // obtains response's output stream
        OutputStream outStream = response.getOutputStream();

        byte[] buffer = new byte[4096];
        int bytesRead = -1;

        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }

        inStream.close();
        outStream.close();