0

我正在考虑使用与发布请求一起传递的图像作为参数。

我无法理解如何使用 java restlet 使用此图像。

我想使用此图像并将其发送打印。

提前致谢。

4

1 回答 1

0

下面是放入一个扩展 ServerResource 的类中的代码,该类在表单的 POST 中调用:

@Post
public Representation upload(Representation entity) throws Exception {
    // 1/ Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();

    // 2/ Create a new file upload handler based on the Restlet
    // FileUpload extension that will parse Restlet requests and
    // generates FileItems.
    RestletFileUpload upload = new RestletFileUpload(factory);

    // 3/ Request is parsed by the handler which generates a
    // list of FileItems
    List<FileItem> items = upload.parseRepresentation(entity);
    String filename = "myImage.png";

    File file = new File(filename);
    for (FileItem fi : items) {
        if (fi.getFieldName().equals("datafile")) {
            System.out.println("Save image ... " + filename);
            fi.write(file);
        }
    }
    // Print your image
    ...
}
于 2013-08-23T08:35:44.380 回答