0

So, I'm a real java beginner.. I'm doing an application with lot of work and researching...

The thing is. I need to post some information with multipart/form-data... I used to do it with Json HashMap. But don't know which object to use instead... Here is my actioncontroller post:

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

Can anyone helps me out??

4
  • 1
    Hi, first of all I would suggest you to check the [spring documentation][1]. Typically multipart is intended for file upload. Do you need to upload a file just as part of an exercise using multipart or maybe it's just a normal request sending that Hash Map? [1]: static.springsource.org/spring/docs/3.0.0.M3/reference/html/… Commented Jan 10, 2013 at 13:10
  • Actually, I'll post it using an other applications api.. But it request that I use multipart... Commented Jan 10, 2013 at 13:14
  • 1
    It's possible to know what other API do you use? Like Apache Commons multipart library, etc Commented Jan 10, 2013 at 14:07
  • 1
    The code you've posted is irrelevant. What's more important is your controller's signature. And most important is what version of Spring you're using. Based on your post, it seems that you're using an older version, or at least not using annotation-driven controllers. The answer will be different depending on version. Commented Jan 10, 2013 at 15:00

1 Answer 1

1

As you are required to upload using multipart I think you must use a File object, specifically the MultipartFile from Spring.

Using Spring you must work in the UI layer using Spring Controllers, it's not necessary to manage the HttpEntity. Just declare the multipart resolver in your configuration file.

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

This is extracted from official Spring 3 Documentation. You can check there some examples. Here I will give you some more: Spring 3 File Upload Example , Spring MVC file upload.

Finally I would suggest you using the MVC pattern. Don't create the DTO and use it's accessors inside the UI layer, create a Service or Facade in the business layer to do that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you jmva!! Worked well for me!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.