首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图片上传图片预览

图片上传图片预览
EN

Stack Overflow用户
提问于 2016-04-15 18:44:37
回答 1查看 496关注 0票数 0

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

Profile Image Upload

EN

回答 1

Stack Overflow用户

发布于 2016-05-12 16:08:45

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

代码语言:javascript
复制
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));
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36645012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档