【问题标题】:getting POST datas in java Spring Rest Server from $.fileDownload从 $.file 下载 java Spring Rest Server 中的 POST 数据
【发布时间】:2014-03-21 12:03:44
【问题描述】:

我使用 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
}

【问题讨论】:

    标签: java spring rest download


    【解决方案1】:

    试试这段代码,它是 jee 而不是 spring,但我认为它几乎相同

        @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 post reauset 需要额外的 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();
        }
    

    配置DTO:

    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;
        }
    }
    

    【讨论】:

    • 但我们使用的是 Spring
    • 我们从哪里得到传递的json字符串
    • Spring使用同一个包javax.ws.rs注解和代码会一样。只需将文件内容写入 OutputStream。
    • 使方法 @POST 添加注释 @Consumes(MediaType.APPLICATION_JSON) 并将您的 json 添加到请求参数。并将其作为参数读入方法
    • 但我没有使用 $.ajax 我正在使用 $.fileDownload
    【解决方案2】:

    如果您在您的服务中使用您的文件作为输出流,您可以执行类似的操作。它在我的一个项目中为我工作。 最重要的部分是:

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

    从响应中你可以得到一些 Streams 和输出类型

    @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 );
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 2023-04-01
      • 2018-09-07
      • 1970-01-01
      • 2016-06-11
      相关资源
      最近更新 更多