简体   繁体   中英

Cannot upload a file with jersey

I did file upload by jersey but both HTML and client would got a ERROR :

415 - Unsupported Media Type

And the server said that:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

@POST
@Path("file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_HTML)
public String uploadFile(
        @FormDataParam("file") InputStream in,
        @FormDataParam("file") FormDataContentDisposition fileDisposition){
    String fullName = fileDisposition.getFileName();
    try {
        OutputStream os = new FileOutputStream(
                new File("D://",fullName));
        int index = 0;
        byte[] buffer = new byte[256];
        while( (index = in.read(buffer)) != -1){
            os.write(buffer , 0 , index);
        }
        in.close();
        os.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    return fullName;
}

So,what can i do?

What a joke.

It needs a jar named mimepull.jar and then the code worked well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM