【问题标题】:Multipart/Form-Data Post - Java SpringMultipart/Form-Data Post - Java Spring
【发布时间】:2013-01-10 13:02:34
【问题描述】:

所以,我是一个真正的 Java 初学者.. 我正在做一个应用程序,需要大量的工作和研究......

事情是这样的。我需要用 multipart/form-data 发布一些信息……我以前用 Json HashMap 来做。但不知道改用哪个对象...这是我的 actioncontroller 帖子:

HashMap<String, ContentDTO> cnt = new HashMap<String, ContentDTO>();

        ContentDTO contentDTO = new ContentDTO();
        contentDTO.setExternal_id("CNT1");
        contentDTO.setTemplate_type_id(103);
        contentDTO.setChannel_id("CHN1");
        contentDTO.setTitle("Conteudo1");
        contentDTO.setText("Conteudo teste 1");
        RulesDTO rules = new RulesDTO();
        SimpleDateFormat publish_date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss-SSS");
        java.util.Date pdate = publish_date.parse("2012-12-28 11:18:00-030");
        java.sql.Timestamp pubdate = new java.sql.Timestamp(pdate.getTime());
        rules.setPublish_date(pubdate);
        SimpleDateFormat expiration_date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss-SSS");
        java.util.Date edate = expiration_date.parse("2013-12-28 11:18:00-030");
        java.sql.Timestamp expdate = new java.sql.Timestamp(edate.getTime());
        rules.setExpiration_date(expdate);
        rules.setNotify_publish(true);
        rules.setNotify_expiration(false);
        rules.setHighlihted(true);

        contentDTO.setRules(rules);

        InteractionsDTO interactions = new InteractionsDTO();
        interactions.setAllow_comment(true);
        interactions.setAuto_download(false);

        contentDTO.setInteractions(interactions);


        cnt.put("content",contentDTO);




        HttpEntity<HashMap<String, ContentDTO>> request = new HttpEntity<HashMap<String, ContentDTO>>(cnt, httpHeaders);

谁能帮帮我?

【问题讨论】:

  • 您好,首先我建议您查看 [spring 文档][1]。通常 multipart 用于文件上传。您是否需要上传文件作为使用 multipart 的练习的一部分,或者它可能只是发送该哈希映射的正常请求? [1]:static.springsource.org/spring/docs/3.0.0.M3/reference/html/…
  • 实际上,我会使用其他应用程序 api 发布它。但它要求我使用 multipart...
  • 有可能知道您还使用了哪些其他 API?如 Apache Commons 多部分库等
  • 您发布的代码无关紧要。更重要的是您的控制器的签名。最重要的是您使用的是什么版本的 Spring。根据您的帖子,您似乎使用的是旧版本,或者至少没有使用注释驱动的控制器。答案会因版本而异。

标签: java spring post multipartform-data


【解决方案1】:

由于您需要使用分段上传,我认为您必须使用 File 对象,特别是 Spring 中的 MultipartFile

使用 Spring 你必须使用 Spring Controllers 在 UI 层工作,没有必要管理 HttpEntity。只需在配置文件中声明多部分解析器即可。

<beans>
<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<!-- Declare explicitly, or use <context:annotation-config/> -->
<bean id="fileUploadController" class="examples.FileUploadController"/>

</beans>

这是从official Spring 3 Documentation 中提取的。您可以查看一些示例。这里我再给你一些:Spring 3 File Upload Example , Spring MVC file upload.

最后我建议你使用 MVC 模式。不要创建 DTO 并在 UI 层中使用它的访问器,而是在业务层中创建一个 Service 或 Facade 来执行此操作。

【讨论】:

  • 谢谢jmva!!对我来说效果很好!
猜你喜欢
  • 2015-09-19
  • 1970-01-01
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 2011-07-28
  • 2019-12-03
相关资源
最近更新 更多