首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Multipart/Form-Data Post - Java Spring

Multipart/Form-Data Post - Java Spring
EN

Stack Overflow用户
提问于 2013-01-10 21:02:34
回答 1查看 2.4K关注 0票数 0

所以,我是一个真正的java初学者..我正在做一个有很多工作和研究的应用程序...

问题是。我需要用multipart/form-data发布一些信息...我曾经和Json HashMap一起做过。但是不知道用哪个对象来代替...这是我的actioncontroller帖子:

代码语言:javascript
复制
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);

有人能帮我吗??

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-10 22:22:10

因为你需要使用多部分上传,所以我认为你必须使用一个文件对象,特别是来自Spring的。

使用Spring时,您必须使用Spring控制器在UI层中工作,而不需要管理HttpEntity。只需在配置文件中声明多部分解析器即可。

代码语言:javascript
复制
<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 ExampleSpring MVC file upload

最后,我建议您使用MVC模式。不要在UI层中创建DTO并使用它的访问器,而是在业务层中创建一个Service或Facade来实现这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14258564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档