0

I have a table which stores attachments uploaded by the users. The attachments can be of type text/doc/jpg etc. Also there would be multiple users uploading files. So there are chances that the file name may be same is some cases. So when this happen the file which is first in the DB table gets downloaded. So along with the file name can one more parameter be added to ensure that correct file is getting downloaded. That other parameter could be the attachment_id which is unique in each case.

This is the action method used to download file on basis of its name

public String downloadAttachFile() throws FileNotFoundException {
   attachFileName = ServletActionContext.getRequest().getParameter("myFileFileName");
   fileInputStream = new FileInputStream(new File(AttachFileName));
   return SUCCESS;

} Thanks In advance.

Pradnya
  • 649
  • 2
  • 6
  • 17
  • It's not clear what you're asking. You can only download a single file at a time; that's just how HTTP works. – Dave Newton Sep 16 '14 at 15:20
  • I have to download a single file only. But into the database there can files with same name. So how how do I handle this? So i was thinking using ID as reference instead of name. Is that possible? – Pradnya Sep 16 '14 at 15:27
  • Of course it's possible. – Dave Newton Sep 16 '14 at 15:36
  • Can u pls tell me how need to edit the method to pass ID – Pradnya Sep 16 '14 at 15:37
  • Get the ID from the request and look up the filename in the DB by whatever the ID is? I don't understand the question--what specifically can't you figure out? How to get a request parameter? Use the normal S2 mechanisms. How to look up data in a DB? – Dave Newton Sep 16 '14 at 15:47
  • M sorry i will just figure out the confusion and get back.I actually dont know how to replace this file name & add id instead . Anyways Thank u for the reply. – Pradnya Sep 16 '14 at 15:54

1 Answers1

0

Not sure where your files are (file system ?) and how can they be stored with the same name (different folders ?), nor when / how you download multiple files at a time, but you just need to replicate the OS mechanism to rename multiple files with the same name with (n) at the end, like foo.txt and foo(1).txt.

Feel free to adapt the function I've written in the answer to my own ZIP question:

// Set-up a list of filenames to prevent duplicate entries
HashSet<String> entries = new HashSet<String>();

/* ====== ====== ====== ====== ====== ====== ====== ====== ====== ====== ======
   then call getUniqueFileName() for each file passing "entries" as parameter
   to get an unique file name.
 ====== ====== ====== ====== ====== ====== ====== ====== ====== ====== ====== */


private String getUniqueFileName(HashSet<String> entries, String completeFileName){                         
    if (entries.contains(completeFileName)){                                                
        int extPos = completeFileName.lastIndexOf('.');
        String extension = extPos>0 ? completeFileName.substring(extPos) : "";          
        String partialFileName = extension.length()==0 ? completeFileName : completeFileName.substring(0,extPos);
        int x=1;
        while (entries.contains(completeFileName = partialFileName + "(" + x + ")" + extension))
            x++;
    } 
    entries.add(completeFileName);
    return completeFileName;
}
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • actually the files are stored into the database. And so it will allow files of same name since there are multiple users. So instead of renaming a file , can I download it on the basis of its ID ?? – Pradnya Sep 16 '14 at 15:25
  • I've used the above function with files coming from the database, to prevent putting them inside a zip with the same name, that is absolutely possible. Please update the question with more details about your workflow – Andrea Ligios Sep 16 '14 at 15:26
  • Are you selecting the files by name? Lol. Select them by I'd by creating a select and a method that accepts an ID instead of filename. Never do anything on DB basing on strings :O – Andrea Ligios Sep 16 '14 at 15:44
  • Ok i will just try and figure whats wrong but thank u for the reply. – Pradnya Sep 16 '14 at 15:54
  • Just pass the Id in the request and use a bytearrayinputatream – Andrea Ligios Sep 16 '14 at 16:10