Vaadin: Downloaded file has whole path as file name

喜你入骨 提交于 2019-12-10 10:56:12

问题


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.


回答1:


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");



回答2:


Here is my code that works fine (downloading a blob from database as a file), but it's using a Servlet and OutputStream rather than DownloadStream in your case:

public class TextFileServlet extends HttpServlet
{
    public static final String PARAM_BLOB_ID = "id";

    private final Logger logger = LoggerFactory.getLogger(TextFileServlet.class);

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException
    {
        Principal userPrincipal = req.getUserPrincipal();
        PersistenceManager pm = PMFHolder.get().getPersistenceManager();
        Long id = Long.parseLong(req.getParameter(PARAM_BLOB_ID));
        MyFile myfile = pm.getObjectById(MyFile.class, id);

        if (!userPrincipal.getName().equals(myfile.getUserName()))
        {
            logger.info("TextFileServlet.doGet - current user: " + userPrincipal + " file owner: " + myfile.getUserName());
            return;
        }

        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition", "attachment;filename=\"" + myfile.getName() + "\"");
        res.getOutputStream().write(myfile.getFile().getBytes());
    }
}

I hope it helps you.




回答3:


StreamResource myResource = createResource(attachmentName);
System.out.println(myResource.getFilename());
if(attachmentName.contains("/"))
   attachmentName = attachmentName.substring(attachmentName.lastIndexOf("/"));
if(attachmentName.contains("\\"))
   attachmentName = attachmentName.substring(attachmentName.lastIndexOf("\\"));
myResource.setFilename(attachmentName);
FileDownloader fileDownloader = new FileDownloader(myResource);
fileDownloader.extend(downloadButton);


来源:https://stackoverflow.com/questions/8169284/vaadin-downloaded-file-has-whole-path-as-file-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!