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