You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mahabir Gupta <ma...@gmail.com> on 2024/03/14 05:38:34 UTC

Re: Unable to open uploaded pdf and docx file

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

Re: Unable to open uploaded pdf and docx file

Posted by Lukasz Lenart <lu...@apache.org>.
śr., 27 mar 2024 o 02:01 Mahabir Gupta <ma...@gmail.com> napisał(a):
>
> Dear Lukasz,
>
> Uploaded files are ok (access them on the server side once they have been
> uploaded). The MD5 for the uploaded file to the server and the downloaded
> file from the server is different. There is a decrease in the number of kb
> when the file is downloaded. The MD5 changed when it passed through the
> following code.
>
> ServletOutputStream out = null;
> FileInputStream fi = null;
> try {
>     out = response.getOutputStream();
>     fi = new FileInputStream(f);
>     byte[] buffer = new byte[(int)f.length]; // Set the buffer size to
> 4KB or any suitable size
>
>     int bytesRead;
>     while ((bytesRead = fi.read(buffer)) != -1) {
>         out.write(buffer, 0, bytesRead);
>     }
> } catch (FileNotFoundException e) {
>     e.printStackTrace();
>     logger.info("File not found error" + filename);
>     logger.error(e.getMessage());
> } catch (IOException e) {
>     logger.info("Error in reading file:" + filename);
> } finally {
>     if (out != null) safeClose(out);
>     if (fi != null) safeClose(fi);
> }

This code isn't related to Struts and as far I see you are missing
.flush() operation
https://github.com/apache/struts/blob/master/core/src/main/java/org/apache/struts2/result/StreamResult.java#L282


Cheers
Lukasz

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Unable to open uploaded pdf and docx file

Posted by Mahabir Gupta <ma...@gmail.com>.
Dear Lukasz,

Uploaded files are ok (access them on the server side once they have been
uploaded). The MD5 for the uploaded file to the server and the downloaded
file from the server is different. There is a decrease in the number of kb
when the file is downloaded. The MD5 changed when it passed through the
following code.

ServletOutputStream out = null;
FileInputStream fi = null;
try {
    out = response.getOutputStream();
    fi = new FileInputStream(f);
    byte[] buffer = new byte[(int)f.length]; // Set the buffer size to
4KB or any suitable size

    int bytesRead;
    while ((bytesRead = fi.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
    logger.info("File not found error" + filename);
    logger.error(e.getMessage());
} catch (IOException e) {
    logger.info("Error in reading file:" + filename);
} finally {
    if (out != null) safeClose(out);
    if (fi != null) safeClose(fi);
}




On Wed, Mar 27, 2024 at 1:51 AM Lukasz Lenart <lu...@apache.org>
wrote:

> I have no idea what can be wrong here. You are using your custom
> download mechanism instead of using Stream result [1] which isn't
> related to Struts itself. I would double check if uploaded files are
> ok (access them on the server side once they have been uploaded).
>
> [1] https://struts.apache.org/core-developers/stream-result.html
>
> pon., 25 mar 2024 o 02:17 Mahabir Gupta <ma...@gmail.com> napisał(a):
> >
> > Dear Lukasz,
> >
> > Any advice will be greatly appreciated.
> > Thank you.
> >
> > Regards
> > Mahabir
> >
> > On Thu, Mar 14, 2024 at 5:03 PM Mahabir Gupta <ma...@gmail.com>
> wrote:
> >
> > > 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 <ma...@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
> > >>
> > >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Unable to open uploaded pdf and docx file

Posted by Lukasz Lenart <lu...@apache.org>.
I have no idea what can be wrong here. You are using your custom
download mechanism instead of using Stream result [1] which isn't
related to Struts itself. I would double check if uploaded files are
ok (access them on the server side once they have been uploaded).

[1] https://struts.apache.org/core-developers/stream-result.html

pon., 25 mar 2024 o 02:17 Mahabir Gupta <ma...@gmail.com> napisał(a):
>
> Dear Lukasz,
>
> Any advice will be greatly appreciated.
> Thank you.
>
> Regards
> Mahabir
>
> On Thu, Mar 14, 2024 at 5:03 PM Mahabir Gupta <ma...@gmail.com> wrote:
>
> > 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 <ma...@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
> >>
> >>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Unable to open uploaded pdf and docx file

Posted by Mahabir Gupta <ma...@gmail.com>.
Dear Lukasz,

Any advice will be greatly appreciated.
Thank you.

Regards
Mahabir

On Thu, Mar 14, 2024 at 5:03 PM Mahabir Gupta <ma...@gmail.com> wrote:

> 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 <ma...@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
>>
>>

Re: Unable to open uploaded pdf and docx file

Posted by Mahabir Gupta <ma...@gmail.com>.
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 <ma...@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
>
>