AbtPst
AbtPst

Reputation: 8018

Cant upload CSV using Websphere

I have a very simple file upload

<form>
                    <table>
                        <tr>
                            <td colspan="2">CSV</td>
                        </tr>
                        <tr>
                            <th>Select File</th>
                            <td><input id="csv" name="csv" type="file" /></td>
                        </tr>
                        <tr>
                            <td colspan="2"><input id="fup" type="submit" value="Upload" /></td>
                        </tr>
                    </table>
                </form>

and here is the ajax call

$("form").submit(function(evt) {

        evt.preventDefault();

        var formData = new FormData($(this)[0]);
        $("#csv").prop("disabled",true);
        $("#fup").prop("disabled",true);
        $.ajax({
            url : 'api/readUpload',
            type : 'POST',
            data : formData,
            async : true,
            cache : false,
            contentType : false,
            enctype : 'multipart/form-data',
            processData : false,
            success : function(response) {
                $('#response').html(response);
                $("img").hide();
                $("#csv").prop("disabled",false);
                $("#fup").prop("disabled",false);
            }
        });
        return false;
    });

on the backend I have

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)

    @Path("/readUpload")
    public void uploadData(@FormParam("meta") String meta, @FormParam("csv") byte[] data) {
        //processing code
    }

My csv file has just one line (for testing)

col_name
some random text,some random text2

now, when i run the code i get

Caused by: java.lang.NumberFormatException: For input string: "col_name
 some random text,some random text2"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65
)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Byte.parseByte(Byte.java:149)
    at java.lang.Byte.valueOf(Byte.java:205)
    at java.lang.Byte.valueOf(Byte.java:231)
    at org.apache.cxf.common.util.PrimitiveUtils.read(PrimitiveUtils.java:81)
    at org.apache.cxf.jaxrs.utils.InjectionUtils.handleParameter(InjectionUtils.jav
a:490)
    ... 41 more

and here is the full stacktrace

[WARNING ] javax.ws.rs.BadRequestException: HTTP 400 Bad Request
    at org.apache.cxf.jaxrs.utils.SpecExceptions.toBadRequestException(SpecExceptio
ns.java:84)
    at org.apache.cxf.jaxrs.utils.ExceptionUtils.toBadRequestException(ExceptionUti
ls.java:121)
    at org.apache.cxf.jaxrs.utils.InjectionUtils.createParamConversionException(Inj
ectionUtils.java:560)
    at org.apache.cxf.jaxrs.utils.InjectionUtils.handleParameter(InjectionUtils.jav
a:496)
    at org.apache.cxf.jaxrs.utils.InjectionUtils.injectIntoCollectionOrArray(Inject
ionUtils.java:1009)
    at org.apache.cxf.jaxrs.utils.InjectionUtils.createParameterObject(InjectionUti
ls.java:1082)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processFormParam(JAXRSUtils.java:966)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.createHttpParameterValue(JAXRSUtils.ja
va:874)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:834)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:786)
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInIn
terceptor.java:255)
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInt
erceptor.java:85)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain
.java:308)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationOb
server.java:124)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDes
tination.java:265)
    at com.ibm.ws.jaxrs20.endpoint.AbstractJaxRsWebEndpoint.invoke(AbstractJaxRsWeb
Endpoint.java:134)
    at com.ibm.websphere.jaxrs.server.IBMRestServlet.handleRequest(IBMRestServlet.j
ava:149)
    at com.ibm.websphere.jaxrs.server.IBMRestServlet.doPost(IBMRestServlet.java:107
)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at com.ibm.websphere.jaxrs.server.IBMRestServlet.service(IBMRestServlet.java:99
)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1
290)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.
java:778)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.
java:475)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilte
rManager.java:1157)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:4956)
    at com.ibm.ws.webcontainer31.osgi.webapp.WebApp31.handleRequest(WebApp31.java:5
25)
    at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2.handleRequest(DynamicVirtu
alHost.java:315)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1014)
    at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2.run(DynamicVirtualHost.jav
a:280)
    at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink$TaskWrapper.r
un(HttpDispatcherLink.java:967)
    at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.wrapHandlerAn
dExecute(HttpDispatcherLink.java:359)
    at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.ready(HttpDis
patcherLink.java:318)
    at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleDiscriminatio
n(HttpInboundLink.java:471)
    at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleNewRequest(Ht
tpInboundLink.java:405)
    at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.processRequest(Http
InboundLink.java:285)
    at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.ready(HttpInboundLi
nk.java:256)
    at com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback.sendToDiscri
minators(NewConnectionInitialReadCallback.java:174)
    at com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback.complete(New
ConnectionInitialReadCallback.java:83)
    at com.ibm.ws.tcpchannel.internal.WorkQueueManager.requestComplete(WorkQueueMan
ager.java:504)
    at com.ibm.ws.tcpchannel.internal.WorkQueueManager.attemptIO(WorkQueueManager.j
ava:574)
    at com.ibm.ws.tcpchannel.internal.WorkQueueManager.workerRun(WorkQueueManager.j
ava:929)
    at com.ibm.ws.tcpchannel.internal.WorkQueueManager$Worker.run(WorkQueueManager.
java:1018)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:11
42)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:6
17)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NumberFormatException: For input string: "col_name 
some random text,some random text2"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65
)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Byte.parseByte(Byte.java:149)
    at java.lang.Byte.valueOf(Byte.java:205)
    at java.lang.Byte.valueOf(Byte.java:231)
    at org.apache.cxf.common.util.PrimitiveUtils.read(PrimitiveUtils.java:81)
    at org.apache.cxf.jaxrs.utils.InjectionUtils.handleParameter(InjectionUtils.jav
a:490)
    ... 41 more
  1. is this the proper way to read csv files with liberty websphere?

  2. Seems like it read the column name and the first line and then it encountered the exception. this is really odd. Could it be due to some character like \r or \n that was written in the file?

Update

I even tried it with an empty file and i get the same result.

Upvotes: 1

Views: 121

Answers (1)

Michael Peacock
Michael Peacock

Reputation: 2104

You're getting a NumberFormatException when your handler is trying to convert your String values "col_name" and "some random text" to a byte array.

Caused by: java.lang.NumberFormatException: For input string: "col_name 
some random text,some random text2"

Simplest fix may be to change this

 @FormParam("csv") byte[] data

To this:

 @FormParam("csv") String[] data

Upvotes: 1

Related Questions