【问题标题】:Upload image to ftp?上传图片到ftp?
【发布时间】:2013-12-22 16:08:36
【问题描述】:

我正在创建一个应用程序,并且我有一张客户的图片。如何获取此图像并上传到 ftp ?

在上传章节阅读 vaadin7 书我做了示例但没有工作,我也在寻找如何将此图像图片发送到 ftp。

我确实试过了。

  /** My UI */
    //foto 
    Image picture = new Image();
    picture.setWidth("128px");
    picture.setHeight("128px");     
    mainLayout.addComponent(foto);

    //upload image picture
    ImageUpload imageUp = new ImageUpload(picture);
    Upload upload = new Upload();
    upload.setCaption("Find your picture");
    upload.setButtonCaption("Send");        
    upload.addSucceededListener(imageUp);
    mainLayout.addComponent(upload);


    /** class upload image picture */
    public class ImageUpload implements Receiver, SucceededListener{
        private File file;
        private Image image;    

    public ImageUpload(Image image){
        this.image = image;
        this.image.setVisible(false);
    }

    @Override
    public void uploadSucceeded(SucceededEvent event) {
        this.image.setVisible(true);
        this.image.setSource(new FileResource(file));
    }

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        FileOutputStream fos = null;
        try{
            file = new File("/tmp/" + filename);            
            fos = new FileOutputStream(file);           
        }catch(final java.io.FileNotFoundException ex){
            new Notification("File not found \n", 
                             ex.getLocalizedMessage(), 
                             Notification.Type.ERROR_MESSAGE)
                             .show(Page.getCurrent());
        }
        return fos;

    }
}

有什么想法吗?

谢谢

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/11739153/…
  • 分享一些无效的细节。澄清一下:您想在图像上传到“webapp服务器”后将图像上传到ftp服务器吗?
  • @nexus,客户何时选择您的图片而不是将其插入到我的数据库中,我认为最好将他的图片发送到 ftp,并在我的数据库中仅插入其名称并将您的图片引用到您的电子邮件中。
  • 我只是想知道:你真的需要 ftp 服务器吗?你的数据库和你的网络服务器在同一台机器上吗?如果是,你真的还需要 ftp 服务器吗?
  • @nexus 我需要这项工作,在 ftp 或其他方式上。我不想在我的数据库中插入客户图像。如果我将图像上传到路径 /home/myapp/images 上的网络服务器,您会怎么想?

标签: java frameworks vaadin vaadin7


【解决方案1】:

根据我们在 cmets 中的对话提供答案:

像您一样使用 Vaadin 的上传组件将图像上传到服务器上的文件夹。

你知道如何在上传时检查扩展名吗,例如 .jpg ?

你可以

  1. 又快又脏:只需检查文件名是否以 .jpg 结尾
  2. Determine上传文件的mime-type
  3. 执行 1. & 2(如果文件不以 .jpg 结尾,则拒绝上传,否则允许上传并重新检查上传的文件及其 mime 类型)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    相关资源
    最近更新 更多