[英]Get data form HTTP POST request with multipart/form-data in Java (Spring)
[英]Multipart/Form-Data Post - Java Spring
因此,我是一名真正的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);
任何人都可以帮我吗?
由于需要使用multipart进行上传,我认为您必须使用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>
摘录自Spring 3官方文档 。 您可以在此处查看一些示例。 在这里,我将为您提供更多信息: Spring 3 File Upload示例 , Spring MVC file upload 。
最后,我建议您使用MVC模式。 不要在UI层内创建DTO并使用它的访问器,而在业务层中创建Service或Facade即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.