使用相对路径使用Jersey在java中存储上传的文件

时间:2016-06-29 08:14:36

标签: java rest jersey

我可以在c://temp/images本地保存图像文件。但我希望它在Project文件夹中。这是我的代码

@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();
            }

        }
}

当我尝试用/resources/Images/替换文件位置时,我收到文件未找到异常。

0 个答案:

没有答案