知道资源的url如何通过Java后台调用浏览器下载

Scroll Down

知道资源的url如和通过java后台通知浏览器通过url下载文件?代码如下

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

@Controller
public class FIleDownLoad {
    @RequestMapping(value = “/download”)
    @ResponseBody
    public void downLoadFile(@RequestParam(value = “url”) String url, HttpServletResponse response) throws Exception {
        new FIleDownLoad().floorOSSDownFile(url, response);
    }
    void floorOSSDownFile(String url, HttpServletResponse response) throws IOException {
        try {
            URL urls = new URL(url);
            URLConnection con = urls.openConnection();
            con.setConnectTimeout(5 * 1000);
            InputStream in = con.getInputStream();
            // 取出文件名称<br />
            String[] strArray = url.split(“/“);
            String fileName = strArray[strArray.length - 1];
            byte[] bs = new byte[1024];
            int len;
            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            //通知浏览器以附件形式下载<br />
            response.setHeader(“Content-Disposition”, “attachment;filename=” + URLEncoder.encode(fileName, “UTF-8”));
            while ((len = in.read(bs)) != -1) {
                out.write(bs, 0, len);
            }
            out.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}