Re: Unable to open uploaded pdf and docx file
śr., 27 mar 2024 o 02:01 Mahabir Gupta 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-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Unable to open uploaded pdf and docx file
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 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 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 > 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 > > > 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()); > > >>
Re: Unable to open uploaded pdf and docx file
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 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 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 > > 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 >> 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 '.': > >>
Re: Unable to open uploaded pdf and docx file
Dear Lukasz, Any advice will be greatly appreciated. Thank you. Regards Mahabir On Thu, Mar 14, 2024 at 5:03 PM Mahabir Gupta 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 > 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> 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)
Re: Unable to open uploaded pdf and docx file
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 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 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
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