通过休息Web服务本地上传文件

时间:2014-05-28 13:51:21

标签: java web-services rest upload jersey

我正在使用此链接http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/中的代码上传文件。在此示例中,我必须从html页面传递以指定要上传的文件,但我想在我调用时加入它webservice的路径(类似于: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();
    }}}

但我现在有405错误!!

修改

@Path(value= "/up")
public class upload {
private static final String SERVER_UPLOAD_LOCATION_FOLDER =    "C://Users/Marwa/Desktop/mafile.png";
@POST
@Path(value="upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadFile(@FormDataParam("file") InputStream fileInputStream) {
    String filePath = SERVER_UPLOAD_LOCATION_FOLDER ;
    System.out.println("*****serverpath********");
    saveFile(fileInputStream, filePath);
    String output = "File saved to server location : " + filePath;
    return output;
}
 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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

我认为你只需要调用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) {
    ...
}