Vaadin:下载的文件具有整个路径作为文件名 [英] Vaadin: Downloaded file has whole path as file name

查看:435
本文介绍了Vaadin:下载的文件具有整个路径作为文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Vaadin应用程序实现了一个下载操作,但由于某些原因,下载的文件具有原始文件的完整路径作为文件名。



任何想法? / p>

您可以看到代码



编辑:



这是代码的重要部分:

  package com.bluecubs.xinco.core.server.vaadin ; 

import com.bluecubs.xinco.core.server.XincoConfigSingletonServer;
import com.vaadin.Application;
import com.vaadin.terminal.DownloadStream;
import com.vaadin.terminal.FileResource;
import java.io. *;
import java.net.URLEncoder;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

/ **
*
* @author Javier A. OrtizBultrón< javier.ortiz.78@gmail.com&
* /
public class FileDownloadResource extends FileResource {

private final String fileName;
私人文件下载;
private File newFile;

public FileDownloadResource(File sourceFile,String fileName,
应用程序应用程序){
super(sourceFile,application);
this.fileName = fileName;
}

protected void cleanup(){
if(newFile!= null&& newFile.exists()){
newFile.delete();
}
if(download!= null&& download.exists()&&& download.listFiles()。length == 0){
download.delete()
}
}

@Override
public DownloadStream getStream(){
try {
//将文件复制到目录下载
InputStream in = new CheckedInputStream(new FileInputStream(getSourceFile()),
new CRC32());
download = new File(XincoConfigSingletonServer.getInstance()。FileRepositoryPath
+ System.getProperty(file.separator)+ UUID.randomUUID()。toString());
newFile = new File(download.getAbsolutePath()+ System.getProperty(file.separator)+ fileName);
download.mkdirs();
OutputStream out = new FileOutputStream(newFile);
newFile.deleteOnExit();
download.deleteOnExit();
byte [] buf = new byte [1024];
int len;
while((len = in.read(buf))> 0){
out.write(buf,0,len);
}
in.close();
out.close();
final DownloadStream ds = new DownloadStream(
new FileInputStream(newFile),getMIMEType(),fileName);
ds.setParameter(Content-Disposition,attachment; filename =
+ URLEncoder.encode(fileName,utf-8));
ds.setCacheTime(getCacheTime());
return ds;
} catch(final FileNotFoundException ex){
Logger.getLogger(FileDownloadResource.class.getName())。log(Level.SEVERE,null,ex);
返回null;
} catch(IOException ex){
Logger.getLogger(FileDownloadResource.class.getName())。log(Level.SEVERE,null,ex);
返回null;
}
}
}

我已经调试并验证fileName只包含文件的名称而不是整个路径。

解决方案

答案其实是houman001的答案和这篇文章的组合: a href =https://vaadin.com/forum/-/message_boards/view_message/200534 =nofollow> https://vaadin.com/forum/-/message_boards/view_message/200534



我从上述方法转到了一个更简单的工作:

  StreamSource ss = new StreamSource(){

byte [] bytes = //在这里获取文件字节
InputStream is = new ByteArrayInputStream(bytes);

@Override
public InputStream getStream(){
return is;
}
};
StreamResource sr = new StreamResource(ss,< file name>< Application Instance>);
getMainWindow()。open(sr,_blank);


I have a download action implemented on my Vaadin application but for some reason the downloaded file has the original file's full path as the file name.

Any idea?

You can see the code on this post.

Edit:

Here's the important part of the code:

package com.bluecubs.xinco.core.server.vaadin;

import com.bluecubs.xinco.core.server.XincoConfigSingletonServer;
import com.vaadin.Application;
import com.vaadin.terminal.DownloadStream;
import com.vaadin.terminal.FileResource;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

/**
 *
 * @author Javier A. Ortiz Bultrón<javier.ortiz.78@gmail.com>
 */
public class FileDownloadResource extends FileResource {

    private final String fileName;
    private File download;
    private File newFile;

    public FileDownloadResource(File sourceFile, String fileName,
            Application application) {
        super(sourceFile, application);
        this.fileName = fileName;
    }

    protected void cleanup() {
        if (newFile != null && newFile.exists()) {
            newFile.delete();
        }
        if (download != null && download.exists() && download.listFiles().length == 0) {
            download.delete();
        }
    }

    @Override
    public DownloadStream getStream() {
        try {
            //Copy file to directory for downloading
            InputStream in = new CheckedInputStream(new FileInputStream(getSourceFile()),
                    new CRC32());
            download = new File(XincoConfigSingletonServer.getInstance().FileRepositoryPath
                    + System.getProperty("file.separator") + UUID.randomUUID().toString());
            newFile = new File(download.getAbsolutePath() + System.getProperty("file.separator") + fileName);
            download.mkdirs();
            OutputStream out = new FileOutputStream(newFile);
            newFile.deleteOnExit();
            download.deleteOnExit();
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            final DownloadStream ds = new DownloadStream(
                    new FileInputStream(newFile), getMIMEType(), fileName);
            ds.setParameter("Content-Disposition", "attachment; filename="
                    + URLEncoder.encode(fileName, "utf-8"));
            ds.setCacheTime(getCacheTime());
            return ds;
        } catch (final FileNotFoundException ex) {
            Logger.getLogger(FileDownloadResource.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(FileDownloadResource.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}

I already debugged and verified that fileName only contains the file's name not the whole path.

解决方案

The answer was actually a mix of houman001's answer and this post: https://vaadin.com/forum/-/message_boards/view_message/200534

I went away from the above approach to a simpler working one:

         StreamSource ss = new StreamSource() {

            byte[] bytes = //Get the file bytes here
            InputStream is = new ByteArrayInputStream(bytes);

            @Override
            public InputStream getStream() {
                return is;
            }
        };
        StreamResource sr = new StreamResource(ss, <file name>, <Application Instance>);
        getMainWindow().open(sr, "_blank");

这篇关于Vaadin:下载的文件具有整个路径作为文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆