views:

371

answers:

1

Hi out there.

I use jaxb in my REST application. I want to send an XML file via a web form. Then the java Class will unmarshal the InputStream.

private void unmarshal(Class<T> docClass, InputStream inputStream)
    throws JAXBException {
    String packageName = docClass.getPackage().getName();
    JAXBContext context = JAXBContext.newInstance(packageName);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    Object marshaledObject = unmarshaller.unmarshal(inputStream);
}

The jsp-File which triggers the unmarshal method has a form which looks like this:

<form action="#" method="POST">
    <label for="id">File</label>
    <input name="file" type="file" />
    <input type="submit" value="Submit" />
</form>

I get the following ParserException :

javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException: Content is not allowed in prolog.].

The question was answered in general here, but i am sure that my file is not corrupt. When i call the code from within a java-Classwith the same file no exception is thrown.

// causes no exception
File file = new File("MyFile.xml");
FileInputStream fis = new FileInputStream(file);
ImportFactory importFactory = ImportFactory.getInstance();
importFactory.setMyFile(fis);

// but when i pass the file with a web form
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response create(@FormParam("file") InputStream filestream) {
    Response response;

    // is a BufferedInputStream, btw    
    LOG.debug("file is type: " + filestream.getClass().getName());

    try { 
        ImportFactory importFactory = ImportFactory.getInstance();
        importFactory.setMyFile(filestream);

        Viewable viewable = new Viewable("/sucess", null);
        ResponseBuilder responseBuilder = Response.ok(viewable);
        response = responseBuilder.build();

    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        ErrorBean errorBean = new ErrorBean(e);
        Viewable viewable = new Viewable("/error", errorBean);
        ResponseBuilder responseBuilder = Response.ok(viewable);
        response = responseBuilder.build();
    }
    return response;
}
A: 

Does the XML header look like the one below?

<?xml version='1.0' encoding='utf-8'?>
navr
Yes it does. As i said: the files used in web-form and in inputstream refer to the same file.
cuh