3

I am able to save image file in c://temp/images locally. But I want it in Project folder itself. Here is my code

@Path("/files")
public class FileUpload {
    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {


            String uploadedFileLocation = "C://temp/images" + fileDetail.getFileName();

            // save it
            writeToFile(uploadedInputStream, uploadedFileLocation);

            String output = uploadedFileLocation;
            return Response.status(200).entity(output).build();

        }

        // save uploaded file to new location
        private void writeToFile(InputStream uploadedInputStream,
            String uploadedFileLocation) {

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

                while ((read = uploadedInputStream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                out.flush();
                out.close();
            } catch (IOException e) {

                e.printStackTrace();
            }

        }
}

When I tried to replace file location with /resources/Images/,I am getting file not found exception.

smali
  • 4,687
  • 7
  • 38
  • 60
Irfan
  • 161
  • 1
  • 12

0 Answers0