Dear Lukasz,

This issue came after I upgraded from Struts 2.5 to Struts 6.0.3.2. The
.pdf and .docx files seem to be corrupted when I am trying to download the
files. Kindly advise. Thank you.

Regards
Mahabir

On Thu, Mar 14, 2024 at 1:38 PM Mahabir Gupta <mahabir...@gmail.com> wrote:

> Dear Lukasz,
>
> I am able to upload and download and open .txt file but for .pdf and .docx
> files, I am able to upload but when I download the pdf file, the file
> cannot be opened. For the .docx file when I try to click on the file, on
> the console it does show the log successfully download attachment file but
> on the frontend an error "System is unable to proceed with your request."
> is being displayed.
>
> public class getDotsMissionAction {
>
>     public void downloadAttach(){
>         try {
>             String filename = 
> DotsFormUtil.cleanStringFile(request.getParameter("filename"));t
>             String realname = 
> DotsFormUtil.cleanStringFile(request.getParameter("name"));
>
>             HttpSession session = (HttpSession) request.getSession();
>             String strDotsIdToken = (String) 
> session.getAttribute("strDotsIdToken");
>
>             try{
>                 if(strDotsIdToken == null || 
> !strDotsIdToken.equals(filename.split("_")[0]))
>                     logger.error("strDotsIdToken is null or strDotsIdToken is 
> not equal and Exception is thrown");
>             }catch (Exception e){
>                 logger.info(e);
>             }
>             try {
>                 if(filename!= null){
>                     try {
>                         ResourceBundle bundle = 
> ResourceBundle.getBundle("resources.dotsDisplay");
>                         String 
> strDirectory=DotsFormUtil.cleanpString(bundle.getString("dots.attachments.path"));
>                         File f= new 
> File(FilenameUtils.normalize(DotsFormUtil.cleanString(strDirectory+File.separator
>  + filename)));
>                         String pattern = 
> "[a-zA-Z0-9]{1,50}\\.[a-zA-Z0-9]{1,10}";
>                         if(realname.matches(pattern)){
>                             response.reset();
>                             response.setCharacterEncoding("UTF-8");
>                             response.setContentType("application/pdf");
>                             
> response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
>                             
> response.setHeader("Content-Disposition","attachment; fileName=" +realname);
>                         }
>                         try (
>                             OutputStream out2 = response.getOutputStream()){
>                                 Files.copy(f,out2);
>                                 out2.flush();
>                             }
>                         } catch (FileNotFoundException e){
>                         e.printStackTrace();
>                         logger.error(e.getMessage());
>                     }
>                 }
>             }catch (FileNotFoundException e){
>                 e.printStackTrace();
>                 logger.error(e.getMessage());
>             }
>             logger.info("successfully download attachment file");
>         }catch (FileNotFoundException e){
>             e.printStackTrace();
>             logger.error(e.getMessage());
>         }
>     }
>
>     public static String cleanStringFile(String- aString){
>         if(aString==null) return null;
>         String cleanString = "";
>         char cleanChar = '\0';
>         for(int i=0; i<aString.length(); i++){
>             cleanChar = cleanCharFile(aString.charAt(i));
>             if(cleanChar != '\0') cleanString+=cleanChar;
>         }
>         return cleanString;
>     }
>
>     private static char cleanCharFile(char aChar){
>         for(int i = 48; i<58; ++i){
>             if(aChar ==i) return (char) i;
>         }
>         for(int i = 65; i<91; ++i){
>             if(aChar ==i) return (char) i;
>         }
>         for(int i = 97; i<123; ++i){
>             if(aChar ==i) return (char) i;
>         }
>
>         switch (aChar){
>             case '.':
>                 return '.';
>             case '_':
>                 return '_';
>             case '-':
>                 return '-';
>             case '!':
>                 return '!';
>         }
>         return '\0';
>     }
>
>
> This is my modified code
>
> public String downloadAttach() {
>     HttpServletResponse response = ServletActionContext.getResponse();
>     try {
>         String filename = 
> DotsFormUtil.cleanStringFile(request.getParameter("filename"));t
>         String realname = 
> DotsFormUtil.cleanStringFile(request.getParameter("name"));
>
>         HttpSession session = (HttpSession) request.getSession();
>         String strDotsIdToken = (String) 
> session.getAttribute("strDotsIdToken");
>
>         try{
>             if(strDotsIdToken == null || 
> !strDotsIdToken.equals(filename.split("_")[0]))
>                 logger.error("strDotsIdToken is null or strDotsIdToken is not 
> equal and Exception is thrown");
>         }catch (Exception e){
>             logger.info(e);
>         }
>     try {
>         if (filename != null) {
>             ResourceBundle bundle = 
> ResourceBundle.getBundle("resources.dotsDisplay");
>             String strDirectory = 
> DotsFormUtil.cleanString(bundle.getString("dots.attachments.path"));
>             File f = new File(strDirectory + File.separator + filename);
>             String pattern = "[a-zA-Z0-9]{1,50}\\.[a-zA-Z0-9]{1,10}";
>             if (realname.matches(pattern)) {
>                 response.reset();
>                 response.setCharacterEncoding("UTF-8");
>
>                 String contentType;
>                 if (realname.toLowerCase().endsWith(".pdf")) {
>                     contentType = "application/pdf";
>                 } else if (realname.toLowerCase().endsWith(".docx")) {
>                     contentType = 
> "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
>                 } else {
>                     contentType = "application/octet-stream";
>                 }
>                 response.setContentType(contentType);
>                 response.setHeader("Content-Disposition", "attachment; 
> filename=" + realname);
>
>                 try (FileInputStream fis = new FileInputStream(f);
>                      OutputStream out2 = response.getOutputStream()) {
>                     byte[] buffer = new byte[1024];
>                     int bytesRead;
>                     while ((bytesRead = fis.read(buffer)) != -1) {
>                         out2.write(buffer, 0, bytesRead);
>                     }
>                 }
>             } else {
>                 // Handle invalid filename pattern
>                 // For example: Log an error, return a response indicating 
> invalid file, etc.
>             }
>         }
>     } catch (IOException e) {
>         e.printStackTrace();
>         return "error"; // Return the result name for error handling
>     }
>
>     return null; // To avoid Struts2 result processing
> }
>
> However I am still not able to download and view .pdf and .docx file.
>
> Kindly assist. Thank you.
>
> Regards
>
> Mahabir
>
>

Reply via email to