FileUpload with JAX-RS

后端 未结 5 1117
忘了有多久
忘了有多久 2020-11-30 05:08

I try to do file upload from a JavaScript client to a JAX-RS Java server.

I use the following REST upload function on my server:

@POST
@Produces(\'ap         


        
5条回答
  •  隐瞒了意图╮
    2020-11-30 05:14

    On Server Side you can use something like this

    @POST
    @Path("/fileupload")  //Your Path or URL to call this service
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
            @DefaultValue("true") @FormDataParam("enabled") boolean enabled,
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {
         //Your local disk path where you want to store the file
        String uploadedFileLocation = "D://uploadedFiles/" + fileDetail.getFileName();
        System.out.println(uploadedFileLocation);
        // save it
        File  objFile=new File(uploadedFileLocation);
        if(objFile.exists())
        {
            objFile.delete();
    
        }
    
        saveToFile(uploadedInputStream, uploadedFileLocation);
    
        String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
    
        return Response.status(200).entity(output).build();
    
    }
    private void saveToFile(InputStream uploadedInputStream,
            String uploadedFileLocation) {
    
        try {
            OutputStream out = null;
            int read = 0;
            byte[] bytes = new byte[1024];
    
            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    
    }
    

    Again this can be checked with the client code in java with

    public class TryFile {
    public static void main(String[] ar)
           throws HttpException, IOException, URISyntaxException {
        TryFile t = new TryFile();
        t.method();
    }
    public void method() throws HttpException, IOException, URISyntaxException {
        String url = "http://localhost:8080/...../fileupload";  //Your service URL
        String fileName = ""; //file name to be uploaded
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        FileBody fileContent = new FiSystem.out.println("hello");
        StringBody comment = new StringBody("Filename: " + fileName);
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("file", fileContent);
        httppost.setEntity(reqEntity);
    
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
    }
    }
    

    With HTML, you can simply check with this code

    
    
    

    Upload File with RESTFul WebService

    Choose a file :

    To get QueryParam, Check @QueryParam or for header param use @HeaderParam

    Example of @QueryParam

    Example of @HeaderParam

    Try this, hope this helps you with your problem.

提交回复
热议问题