0

I'm trying to upload files to a web service using curl, then save those files in a database. It's working well so far for text files, but pictures are corrupted.

So let's say I'm uploading the file logo.png using curl :

curl -T D:\logo.png http://127.0.0.1:8080/my-web-service/resteasy/document/metadata?id=logo"&"contentType=IMAGE"&"httpType=image"/"png

My webservice consumes the data like this:

@Path("metadata")
@PUT
public void putDocument(
    @QueryParam("id") String docId,
    @DefaultValue("GENERIC_CONTENT") @QueryParam("contentType") String contentType,
    @DefaultValue("text/html") @QueryParam("httpType") String httpType,
    byte[] docContent) {
Document doc = new Document();
doc.setDocumentId(docId);
doc.setContentType(new RefContentType(ContentType.valueOf(contentType)));
doc.setHttpContentType(httpType);
doc.setContent(docContent);
doc = getDAO().save(doc);
LOG.info("Document saved successfully id : " + doc.getId());

So my best guess is that the setContent method doesn't work well with a byte array containing something other than plain text. How can I know if curl is messing the upload or if it's my ejb that reads the array badly?

Thanks for your help.

Lupuss
  • 649
  • 2
  • 11
  • 21

1 Answers1

0

Turns out the uploading worked fine. i could confirm this by saving the array on my disk as an image file using Java BufferedImage class.

The problem lies in Hibernate's annotations.

My byte array was saved in an Oracle blob using this Java variable:

@Column(name="CONTENT")
@Lob //@Basic(fetch=FetchType.LAZY)
byte[] content= null;

The @Lob and @Basic annotations are not working properly together. For more explanation on this see this question : Spring, Hibernate, Blob lazy loading

Community
  • 1
  • 1
Lupuss
  • 649
  • 2
  • 11
  • 21