Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6878223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:41:49+00:00 2026-05-27T04:41:49+00:00

I am trying to upload a picture, and save it in database. If I

  • 0

I am trying to upload a picture, and save it in database.
If I do the following I get this error:

Failed to convert property value of type

org.springframework.web.multipart.commons.CommonsMultipartFile to
required type java.lang.Byte[] for property picture1; nested exception
is java.lang.IllegalArgumentException: Cannot convert value of type
[org.springframework.web.multipart.commons.CommonsMultipartFile] to
required type [java.lang.Byte] for property picture1[0]:
PropertyEditor
[org.springframework.beans.propertyeditors.CustomNumberEditor]
returned inappropriate value

If i do it this way:

if(request instanceof MultipartHttpServletRequest){
    MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
    CommonsMultipartFile f = (CommonsMultipartFile) mpr.getFile("picture1");
}

I get this error:

Executing action [save] of controller [com.testapp.RequestController]
caused exception: Cannot cast object ‘
org.springframework.web.multipart.commons.CommonsMultipartFile@34ae1f02’
with class
‘org.springframework.web.multipart.commons.CommonsMultipartFile’ to
class ‘java.lang.Byte’

What should I do to make this work?

Domain

package com.testapp

class Request{  
    String requestID
    Date dateCreated
    String subject
    String startedBy 
    String description
    String status
    String priority 
    Productline productline
    Topic topic
    Subtopic subtopic
    String company
    Byte [] picture1
    Byte [] picture2
    Byte [] picture3
    String acceptedBy 


    static constraints = {
        requestID(blank:true,nullable:true)
        dateCreated(blank:true,nullable:true)
        subject()
        description(maxSize:5000)
        status (blank:true,nullable:true)
        priority(inList:["Normal","Urgent","Not urgent"])
        productline(blank:true,nullable:true)
        topic(blank:true,nullable:true)
        subtopic(blank:true,nullable:true)
        company(blank:true,nullable:true)
        startedBy(blank:true,nullable:true)
        acceptedBy(blank:true,nullable:true)
        picture1(blank:true,nullable:true)
        picture2(blank:true,nullable:true)
        picture3(blank:true,nullable:true)
    }

}

GSP:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="layout" content="main" />
    <g:set var="entityName" value="${message(code: 'request.label', default: 'Request')}" />
    <title><g:message code="New request" args="[entityName]" /></title>
</head>
<body>
    <div class="nav">
        <span class="menuButton"><a class="home" href="${createLink(uri: '/')}"><g:message  `code="default.home.label"/></a></span>`
        <span class="menuButton"><g:link class="list" action="userList"><g:message code="Lista zahteva" `args="[entityName]" /></g:link></span>`
    </div>
<div class="body">
        <h1><g:message code="New request" args="[entityName]" /></h1>
        <g:if test="${flash.message}">
        <div class="message">${flash.message}</div>
        </g:if>
        <g:hasErrors bean="${requestInstance}">
        <div class="errors">
            <g:renderErrors bean="${requestInstance}" as="list" />
        </div>
</g:hasErrors>
        <g:form action="save" method="post" enctype="multipart/form-data">
            <div class="dialog">
                <table>
                    <tbody>

                        <tr class="prop">
                            <td valign="top" class="name">
                                <label for="subject"><g:message code="request.subject.label" default="Subject" /></label>
                            </td>
                            <td valign="top" class="value ${hasErrors(bean: requestInstance, field: 'subject', 'errors')}">
                                <g:textField name="subject" value="${requestInstance?.subject}" />
                            </td>
                        </tr>

                        <tr class="prop">
                            <td valign="top" class="name">
                                <label for="description"><g:message code="request.description.label" default="Opis" /></label>
                            </td>
                            <td valign="top" class="value ${hasErrors(bean: requestInstance, field: 'description', 'errors')}">
                                <g:textArea name="description" cols="40" rows="5" value="${requestInstance?.description}" />
                            </td>
                        </tr>
                        <tr>
                            <td valign="top" class="name">
                                <label for="picture1"><g:message code="request.picture1.label" default="Printscreen" /></label>
                            </td>           

                            <td valign="top" class="value">
                                <input type="file" id="picture1" name="picture1"/>
                            </td>
                        </tr>

                        <tr class="prop">
                            <td valign="top" class="name">
                                <label for="priority"><g:message code="request.priority.label" default="Priority" /></label>
                            </td>
                            <td valign="top" class="value ${hasErrors(bean: requestInstance, field: 'status', 'errors')}">
                                <g:select name="priority" from="${requestInstance.constraints.priority.inList}" value="${requestInstance?.priority}" valueMessagePrefix="request.priority"  />
                            </td>
                        </tr>



                        <tr class="prop">
                            <td valign="top" class="name">
                                <label for="productline"><g:message code="request.productline.label" default="Productline" /></label>
                            </td>
                            <td valign="top" class="value ${hasErrors(bean: requestInstance, field: 'productline', 'errors')}">
                                <g:select name="productline.id" from="${com.testapp.Productline.list()}" optionKey="id" value="${requestInstance?.productline?.id}"  />
                            </td>
                        </tr>

                        <tr class="prop">
                            <td valign="top" class="name">
                                <label for="topic"><g:message code="request.topic.label" default="Topic" /></label>
                            </td>
                            <td valign="top" class="value ${hasErrors(bean: requestInstance, field: 'topic', 'errors')}">
                                <g:select name="topic.id" from="${com.testapp.Topic.list()}" optionKey="id" value="${requestInstance?.topic?.id}"  />
                            </td>
                        </tr>

                        <tr class="prop">
                            <td valign="top" class="name">
                                <label for="subtopic"><g:message code="request.subtopic.label" default="Subtopic" /></label>
                            </td>
                            <td valign="top" class="value ${hasErrors(bean: requestInstance, field: 'subtopic', 'errors')}">
                                <g:select name="subtopic.id" from="${com.testapp.Subtopic.list()}" optionKey="id" value="${requestInstance?.subtopic?.id}"  />
                            </td>
                        </tr>

                    </tbody>
                </table>
            </div>
            <div class="buttons">
                <span class="button"><g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" /></span>
            </div>
        </g:form>
        </div>
</body>

Controller:

def save = {
    def requestInstance = new Request(params)
    def requestNumberInstance = new RequestNumber()
            def upPic1 = request.getFile('picture1') 

    def lastReqNum = RequestNumber.find("from RequestNumber ORDER BY requestNumber desc")
    if(lastReqNum){
        requestNumberInstance.requestNumber = lastReqNum.requestNumber + 1
    }
    else{
        requestNumberInstance.requestNumber = 110000
    }


    requestInstance.requestID = "CSC" + requestNumberInstance.requestNumber
    def currentUserContact =  Contact.findByUser(springSecurityService.getCurrentUser())
    requestInstance.startedBy = currentUserContact.realname
    requestInstance.company = currentUserContact.company
    requestInstance.status = "Opened"
    requestInstance.acceptedBy = "Not yet accepted"
            requestInstance.picture1 = upPic1


    if(requestNumberInstance.save()){



        if (requestInstance.save()) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'request.label', default: 'Request'), requestInstance.id])}"
            redirect(action: "show", id: requestInstance.id)
        }
        else {
            render(view: "create", model: [requestInstance: requestInstance])
        }
    }
    else{
         render(view: "create", model: [requestInstance: requestInstance])
    }
}

Please dont mind the spaghetti code. I’m just trying to get some basic concepts. I will clear it later.

Simplified example:

def save = {
    def requestInstance = new Request(params)
    def requestNumberInstance = new RequestNumber()

    if(requestInstance.validate() && requestInstance.save(flush: true)){
        println "Saved successfully with ${requestInstance.picture1.length} bytes"
    } 
    else {
        println "Save failed"
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:41:50+00:00Added an answer on May 27, 2026 at 4:41 am

    It only worked when I changed my domain properties picture1,picture2,picture3 to:

    byte [] picture1
    byte [] picture2
    byte [] picture3
    

    and added those mappings:

        static mapping = {
        picture1 column:"picture1", sqlType: "blob"
        picture2 column:"picture2", sqlType: "blob"
        picture3 column:"picture3", sqlType: "blob"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to upload a picture/imageview to php server. This server for uploading photos
I am trying to upload an image,create thumbnail but i get an error. Here
I'm trying to upload an application to the iPhone App Store, but I get
I'm trying to upload a picture to a specific website using php cURL but
I'm currently trying to upload a picture to a server with the FileTransfer.upload() method
been trying to search the reason for this error for a long time and
When trying to upload a picture to twitter along with text(of 140 char), i
I am trying to design an Picture Upload feature into a web site. I
I'm trying to set up an NSinvocation to upload a picture to a website.
I'm having a user upload a picture, I'm trying to allow them to stay

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.