下载并转发xls

时间:2016-04-14 18:45:23

标签: java spring xls

我已经完成了一项服务,应该下载一个excelfile并传递给用户。 下载方法:

private void fetchCompanyInfo(int id) throws IOException{
    URL url = new URL("https://xxxxxxxxxx");


    URLConnection con = url.openConnection();
    String query = "ID="+java.net.URLEncoder.encode(String.valueOf(id),"UTF-8"); 

    String cookie = "";
    for (String tmp: this.cookies)
    {
        System.out.println(tmp);
        cookie += tmp.split(";")[0] + ";";
        System.out.println(cookie);
    }

    con.setRequestProperty(COOKIE, cookie);
    System.out.println("this is the cookie: " + cookie);
    con.setRequestProperty("Content-length", String.valueOf(query.length())); 
    con.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
    con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)"); 
    con.setDoOutput(true); 
    con.setDoInput(true); 


    OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
    out.write(query);
    out.close();


    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String receive = "";
    do {
        String line = in.readLine();
        if (line == null)
            break;
        receive += line;
    } while (true);
    in.close();

    excel = receive;
    System.out.println(receive);

}

这是控制器:

@RequestMapping(value="/test", produces="application/x-msexcel")
public CompanyInfo test() throws IOException {        
    return new CompanyInfo();
}

网站im调用的响应返回 内容 - 处理:附件;文件名= Company123.xls; 内容类型:Application / x-msexcel 所以我只想转发那个附件

0 个答案:

没有答案