-2

I would like to exclude execution extensions files when I upload files(ex:"exe","java",...) I wrote the condition but after that Idk how to do catch things with it! I would like to show a alert message when I upload file. Is there any way that I could do?

@Component("fileUtils")
public class FileUtil {
String filePath = "/Dev/file/";
String imgPath = "/Dec/img/";

//create file
public void parseInsertFileInfo(HttpServletRequest request, FileInfoVo boardEntity) throws Exception{
    MultipartHttpServletRequest mServletRequest = (MultipartHttpServletRequest) request;
    Iterator<String> iterator = mServletRequest.getFileNames();

    MultipartFile mFile = null;
    String orgFilename = null;
    String filename = null;
    String orgFileExtension = null;
    String sysFilename = null;

    int boardIdx = 1;

    //make dir when there is no dir
    File file = new File(filePath);
    if(!file.exists()){
        file.mkdirs();
    }


    while(iterator.hasNext()){
        mFile = mServletRequest.getFile(iterator.next());
        if(!mFile.isEmpty()){
            orgFilename = mFile.getOriginalFilename(); 
            orgFileExtension = orgFilename.substring(orgFilename.lastIndexOf(".")); //.ext
            filename = orgFilename.substring(0, orgFilename.lastIndexOf(".")); //file name

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_hhmmss");
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia"));
            sysFilename = filename + sdf.format(calendar.getTime()) + orgFileExtension; // sysFilename : filename + time(h,m,s) + .ext

            String execExt = "#.exe#.zip#.java#"; 
            String imgExt = "#.jpg#.jpeg#"; 


            if(execExt.contains(orgFileExtension)){

            } else if(imgExt.contains(orgFileExtension)){
                filePath = "/Dev/img/";
                file = new File(filePath + sysFilename);
                mFile.transferTo(file);
            } else{
                filePath = "/Dev/file/";
                file = new File(filePath + sysFilename);
                mFile.transferTo(file);
            }
        }
    }
}
Julia
  • 1
  • 2
  • Your question is not clear...you can regex to exclude certain files...check this http://stackoverflow.com/questions/23178316/regular-expression-for-excluding-file-types-exe-and-js – saurav Jul 11 '16 at 09:04
  • anyway, why do you throw an exception if it is just after catched for a redirect? just do the redirect directly without a try/catch – ddb Jul 11 '16 at 09:51
  • Sorry English is not my first language so that my explanation was not clear. I just wanted to exclude some file extensions. I know there is a way that I could do it using javascript but I got class that makes files and stuff so I thought I could do it here. Maybe I will add more codes! – Julia Jul 11 '16 at 23:38

1 Answers1

0

You do not need to throw and catch that Exception. Your way of determining if the extension is in your string should work, an alternative would be regular expressions.

Try it like this (I assume "List.do" is what you would like to send back)

String execExt = "#.exe#.zip#.java#";

//orgFileExtension is a variable which contains extension name(".txt",".jpg",...)
if(execExt.contains(orgFileExtension)){    
    sendRedirect("List.do"); 
}
ageh
  • 91
  • 10