如何从POST调用参数中读取图像

时间:2013-06-03 16:32:19

标签: java restlet

我正在考虑使用已将传递请求作为参数传递的图像。

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

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

提前致谢。

1 个答案:

答案 0 :(得分: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
    ...
}