Java 通过RESTWeb服务本地上传文件

Java 通过RESTWeb服务本地上传文件,java,web-services,rest,upload,jersey,Java,Web Services,Rest,Upload,Jersey,我正在使用此链接中的代码上载文件。在本例中,我必须从html页面传递来指定要上载的文件,但当我通过路径调用Web服务时,我想访问该文件(例如:http://******:8080/RESTfulExample/file/upload/C://image.png) 对这个问题有什么建议吗?请帮忙! 这就是我到现在为止解决这个问题所做的 @Path(value="/files") public class upload { @POST @Path(value = "upload/{path}") @

我正在使用此链接中的代码上载文件。在本例中,我必须从html页面传递来指定要上载的文件,但当我通过路径调用Web服务时,我想访问该文件(例如:http://******:8080/RESTfulExample/file/upload/C://image.png) 对这个问题有什么建议吗?请帮忙! 这就是我到现在为止解决这个问题所做的

@Path(value="/files")
public class upload {
@POST
@Path(value = "upload/{path}")
@Consumes("image/jpg")
public Response uploadPng(@PathParam("path") String path, File file) throws IOException     {
    file = new File("path");
    String uploadedFileLocation = "C:/Users/Desktop/" + file.getName();
    DataInputStream diStream =new DataInputStream(new FileInputStream(file));
    long len = (int) file.length();
    byte[] fileBytes = new byte[(int) len];
    int read = 0;
    int numRead = 0;
    while (read < fileBytes.length && (numRead =
            diStream.read(fileBytes, read,fileBytes.length - read)) >= 0) {
        read = read + numRead;
    }
    writeToFile(diStream, uploadedFileLocation);
    System.out.println("File uploaded to : " + uploadedFileLocation);
    return Response.status(200).entity(file).build();
  }
  private void writeToFile(InputStream uploadedInputStream,
                         String uploadedFileLocation) {
    try {
        OutputStream out =new FileOutputStream(new File(uploadedFileLocation));
        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();
    }}}

}我想你应该打电话给
http://example.com/file/upload
并使用浏览器(使用JavaScript)或其他客户端将文件发布到那里。例如,您可以使用curl测试它

curl -i -F "file=@/home/user1/Desktop/test.jpg" http://example.com/file/upload
您需要服务器端的文件路径吗?如果出于某种原因需要服务器端的路径,只需添加一个
@PathParam

@POST
@Path("/upload/{path}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
    @PathParam("path") String path, 
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail) {
    ...
}
您还可以尝试停止使用
@consumes
或使用特定类型,如
@consumes(“image/jpg”)
。例如:

@POST
@Path("/upload/{path}")
@Consumes("image/jpg")
public Response uploadFile(
    @PathParam("path") String path, 
    InputStream uploadedInputStream) {
    ...
}

我需要帮助!!任何人都可以回答:(但是这里@jcragun(最后一个例子)什么是uploadedInputStream?这是实际的文件-一个字节流。我编辑了我的帖子,所以你可以看到我的代码。请检查它@jcragun你发布的是
png
,但只允许
jpg
。试试
@Consumes(“image/png”)
。仍然不走运吗?我尝试运行该示例,但出现“状态415-不支持的媒体类型”错误。
@POST
@Path("/upload/{path}")
@Consumes("image/jpg")
public Response uploadFile(
    @PathParam("path") String path, 
    InputStream uploadedInputStream) {
    ...
}