图像上传图像预览

时间:2016-04-15 10:44:37

标签: java vaadin

大家希望你能帮助我在按下上传按钮后选择图像后显示预览图像或缩略图。希望你能帮助我们!感谢..

Profile Image Upload

1 个答案:

答案 0 :(得分:0)

这是我的上传处理程序代码 这里 temp_storage_path 是本地应用程序临时路径

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadFinishedHandler;

public class ImageUploadFinishedHandler implements UploadFinishedHandler {

    VerticalLayout imageLayout;

    public ImageUploadFinishedHandler(VerticalLayout imageLayout) {
        this.imageLayout = imageLayout;
    }

    @Override
    public void handleFile(InputStream stream, String fileName, String arg2, long arg3) {
        File file = null;
        try {
            file = new File("temp_storage_path"+fileName);
            OutputStream outputStream = new FileOutputStream(file);
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = stream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
            outputStream.close();
        } catch (FileNotFoundException e) {
            return;
        } catch (IOException e) {
            return;
        }
        this.imageLayout.removeAllComponents();

        Image previewImage = new Image();
        this.imageLayout.addComponent(previewImage);
        previewImage.setWidth("100px");
        previewImage.setHeight("100px");
        previewImage.setSource(new FileResource(file));
    }

}