yogev levi
yogev levi

Reputation: 377

how to Upload File using Jersey

I'm trying to create service for uploading files.

I follow the guide https://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/

but when i'm trying to run i get

Status Code:405 Method Not Allowed

what i'm doing wrong?

here is my code

server

@Path("/doc")
public class DocResource extends BaseResource<DocDao, DocEntity>
{
    @POST
    @Path("/uploadDoc")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String uploadFile(@Context HttpServletRequest req,
            @FormDataParam("file") InputStream fileInputStream,
            @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {

        String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();

        // save the file to the server
        saveFile(fileInputStream, filePath);

        String output = "File saved to server location : " + filePath;

        return output;

    }

    // save uploaded file to a defined location on the server
    private void saveFile(InputStream uploadedInputStream,
            String serverLocation) {

        try {
            OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            outpuStream = new FileOutputStream(new File(serverLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                outpuStream.write(bytes, 0, read);
            }
            outpuStream.flush();
            outpuStream.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

}

HTML

<div class="modal fade" id="addEditDoc" tabindex="-1" role="dialog" aria-labelledby="addEditDoc" data-backdrop="false" data-keyboard="false">    
    <div class="modal-dialog addEditDocModal" role="document">
        <div class="modal-content myModal">    
            <h1>Upload a File</h1>

                <form action="http://127.0.0.1:8080/maintenance/uploadDoc" method="GET" enctype="multipart/form-data">

                   <p>
                        Select a file : <input type="file" name="file" size="50" />
                   </p>

                   <input type="submit" value="Upload It" />
                </form>
        </div>
    </div>    
</

Upvotes: 0

Views: 1092

Answers (1)

Badr
Badr

Reputation: 10658

I think the method must be POST instead of GET

Please find the further Documentation Here

 <div class="modal-dialog addEditDocModal" role="document">
        <div class="modal-content myModal">    
            <h1>Upload a File</h1>

                <form action="http://127.0.0.1:8080/maintenance/doc/uploadDoc" method="POST" enctype="multipart/form-data">

                   <p>
                        Select a file : <input type="file" name="file" size="50" />
                   </p>

                   <input type="submit" value="Upload It" />
                </form>
        </div>
    </div> 

Upvotes: 1

Related Questions