从$ .file下载获取java Spring Rest Server中的POST数据

时间:2014-03-21 12:03:44

标签: java spring rest download

我使用 jQuery File Download v1.2.0 插件编写了下载文件的应用程序。 在jQuery File Download v1.2.0中,我们有一个选项,用于发送数据作为POST

包含数据和查询字符串的POST请求

$.fileDownload('/GetAFoo?a=b', { httpMethod : "POST", data: { foo : "bar"}})

任何人都可以告诉我如何在服务器端接收发布数据,我使用Java Spring Rest Server

我的代码如下所示

脚本

var json ="[[\"Name\",\"Place\",\"Phone\"]," + "[\"AAA\",\"hhh\",\"123\"]," + "[\"BBB\",\"\",\"254\"]," + "[\"CCC\",\"'#?\",\"\"]]";
    $.fileDownload(URL,{ httpMethod : "POST", data: { excelContent : json}}).done(function(e, response)
    {
    }).fail(function(e, response)
    {
    });

Java Spring Rest

@RequestMapping(value = "fileDownload", method = RequestMethod.GET, produces = APP_JSON)
@ResponseBody
public void getMyFileDownload(final HttpServletResponse response) throws IOException, Exception
{
        //how to get the content of excelContent over here
}

2 个答案:

答案 0 :(得分:0)

试试这个代码,它不是春天,但我认为它几乎相同

    @GET
    @Path("download")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response exportScenario(@Context final HttpServletResponse resp) {
        resp.setHeader("Cache-Control", "public");
        resp.setHeader("Pragma", "public");
        return Response.ok(new StreamingOutput() {
            public void write(final OutputStream os) throws IOException {
                // place to write file data into stream
            }
        }).header("Content-Disposition", "attachment; filename=\"file.zip\"").build();
    }

更新: 浏览器方面:

$.ajax({
    url : 'rest/configuration/add.json',
    type : 'POST',
    contentType: 'application/json',
    data : $.toJSON({
        title : $('#conf_create_name')[0].value, 
        comment : $('#conf_create_comment')[0].value,
        params : params
    }),
});

使用json对象$ .toJSON重新发布Ajax后需要额外的jquery插件搜索google。

服务器端:

    @POST
    @Path("download")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response exportScenario(@Context final HttpServletResponse resp, final ConfigurationDTO confDTO) {
        resp.setHeader("Cache-Control", "public");
        resp.setHeader("Pragma", "public");
        return Response.ok(new StreamingOutput() {
            public void write(final OutputStream os) throws IOException {
                // place to write file data into stream
            }
        }).header("Content-Disposition", "attachment; filename=\"file.zip\"").build();
    }

ConfigeratioDTO:

public class ConfigurationDTO {
    private String title;
    private String comment;
    private Map<String, String> params;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }
}

答案 1 :(得分:0)

如果您收到服务中的文件,则可以将其归档为输出流,您可以执行类似的操作。在我的一个项目中,它正在为我工​​作。 最重要的部分是:

OutputStream out = response.getOutputStream();
fileStream.writeTo( out );

从响应中可以获得一些流和输出类型

@RequestMapping(method = RequestMethod.GET, value = "/getFile")
    public void getFile(FilterDTO filter, HttpServletResponse response) {

        ByteArrayOutputStream fileStream = yourService.returnFile( filter );

        try {
            String fileNamePdf = "FileName.pdf";
            response.setHeader( "Content-disposition", "inline; filename=" fileNamePdf );
            response.setContentType("your application/pdf or xls or extension" );

            OutputStream out = response.getOutputStream();
            fileStream.writeTo( out );

            response.setStatus( HttpServletResponse.SC_OK );
        } catch (IOException ignored) {
            response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
            throw new Exception( //your exception implementation );
        }
    }

对您的代码进行一些调整:

@RequestMapping(method = RequestMethod.GET, value = "/fileDownload")
    public void getMyFileDownload(HttpServletResponse response) {

        ByteArrayOutputStream fileStream = yourService.returnFile();

        try {
            String fileNamePdf = "FileName.pdf";
            response.setHeader( "Content-disposition", "inline; filename=" fileNamePdf );
            response.setContentType("your application/pdf or xls or extension" );

            OutputStream out = response.getOutputStream();
            fileStream.writeTo( out );

            response.setStatus( HttpServletResponse.SC_OK );
        } catch (IOException ignored) {
            response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
            throw new Exception( //your exception implementation );
        }
    }