Vaadin: The downloaded file has all the way as the file name

I have a download action implemented in my Vaadin application, but for some reason the downloaded file has the original full path of the file as the file name.

Any idea?

You can see the code in 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 have already debugged and checked that the file_name contains only the file name and not the whole path.

+2
source share
3 answers

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

I walked away from the above approach to a simpler worker:

  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"); 
+4
source

Here is my code that works fine (loading blob from a database as a file), but using Servlet and OutputStream, not 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()); } } 

Hope this helps you.

+1
source
 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); 
0
source

All Articles