Java tutorial
package com.dwf.tastyroad.controller; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.test.context.ContextConfiguration; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ModelAndView; import com.dwf.tastyroad.model.Page; import com.dwf.tastyroad.model.Restaurant; import com.dwf.tastyroad.model.Search; import com.dwf.tastyroad.service.RestaurantService; import com.oreilly.servlet.MultipartRequest; import com.oreilly.servlet.multipart.FilePart; import com.oreilly.servlet.multipart.MultipartParser; import com.oreilly.servlet.multipart.ParamPart; import com.oreilly.servlet.multipart.Part; import com.oreilly.servlet.multipart.DefaultFileRenamePolicy; /*: RequestMapping .do . by .*/ //Class Controller? Spring? @Controller //URI /restaurant @RequestMapping("/restaurant") //classpath(src/main/resources)/config/commonservice.xml?? ?(pageSize, pageUnit) @ContextConfiguration(locations = { "classpath:config/commonservice.xml" }) public class RestaurantController { //commonservice.xml? ?? commonProperties(classpath:config/commonservice.xml)? //pageUnit ? field? injection @Value("#{commonProperties['pageUnit']}") private int pageUnit; //??. pageSize @Value("#{commonProperties['pageSize']}") private int pageSize; //restaurantServiceImpl? restaurantService? Wiring(DI:dependancy injection) @Autowired @Qualifier("restaurantServiceImpl") private RestaurantService restaurantService; /*field (for ftp)*/ private static final String SERVER_IP = "119.205.211.176"; private static final int PORT = 5001; private static final String ID = "imageserver"; private static final String PASSWORD = "WeAre47th"; private static final String UPLOAD_DIR = "teamdwf"; public static HashMap<String, File> fileMap = new HashMap<String, File>(); private Set<String> set = null; private String[] keys = null; ///Constructor - default public RestaurantController() { //Controller wiring? ? ?? console? ( ) System.out.println("::" + getClass() + " Default Constructor Call"); } //listRestaurant - ? @RequestMapping(value = "/getRestaurantListView", method = RequestMethod.POST) //Search ?? ?? ? - web client ? public ModelAndView getRestaurantListViewAction(@ModelAttribute("search") Search search) throws Exception { System.out.println("_______________________________________________"); System.out.println("==> /restaurant/getRestaurantListView__call !!!"); System.out.println("==> search : " + search); System.out.println("_______________________________________________"); //CurrentPage 1 set if (search.getCurrentPage() == 0) { search.setCurrentPage(1); } //field? ? pageSize set search.setPageSize(pageSize); //DB? (? null)? ? restaurant ? List restaurantList = restaurantService.getRestaurantList(search); //Page instance ?(?? page navigation ) Page resultPage = new Page(search.getCurrentPage(), restaurantService.getTotalCount(search), pageUnit, pageSize); System.out.println("=============" + search.getCurrentPage() + "============="); //restaurant list console? Restaurant restaurant; for (int i = 0; i < restaurantList.size(); i++) { restaurant = (Restaurant) restaurantList.get(i); System.out.println("restaurant.toString()" + restaurant.toString() + "__"); } System.out.println("@/listRestaurant resultPage : " + resultPage); //response? model view ? ModelAndView ? ? ModelAndView modelAndView = new ModelAndView(); // view set. //WEB-INF/restaurantViews/restaurantList.jsp (servlet-context.xml) modelAndView.setViewName("restaurantViews/restaurantList"); //? 3??. ?,? response? modelAndView.addObject("restaurantList", restaurantList); modelAndView.addObject("resultPage", resultPage); modelAndView.addObject("search", search); //? modelAndView return ? ? set? view ?? return modelAndView; } ///getAddRestaurantView - @RequestMapping(value = "/getAddRestaurantView", method = RequestMethod.POST) public ModelAndView getAddRestaurantViewAction(@ModelAttribute("restaurant") Restaurant restaurant) throws Exception { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("restaurantViews/restaurantAdd"); return modelAndView; } ///getAddRestaurantView - @RequestMapping(value = "/addRestaurant", method = RequestMethod.POST) //?? web client? ? restaurant? ? public ModelAndView addRestaurantAction(@ModelAttribute("restaurant") Restaurant restaurant, HttpServletRequest request, HttpServletResponse response) throws Exception { //Field String fileDir1 = request.getRealPath("/") + "resources/img"; int max = 5 * 640 * 480; String fileInput = ""; String fileName = ""; String fileDir2 = ""; System.out.println("_______________________________________________"); System.out.println("==> /restaurant/addRestaurant __call !!!"); System.out.println("_______________________________________________"); //=============================================<MultipartRequest>======================================== // // +---------------------+ // | author : . | // +---------------------+ // | Date : 2014-07-02 | // +---------------------+ // // 1. ? ? MultipartRequest . // 2. restaurantAdd.jsp? file? Controller? MultipartRequest . // 3. restaurantAdd.jsp ? 2. // form enctype="multipart/form-data" ? . // ? input ? type="file"? ? . // 4. MultipartRequest . // MultipartRequest ? ? File? Local File System? . // Enumeration file form name & file name? . (while? ??) // File? new File()? , fileName? ?? . // key-value ? <String, File> map? , File?? .(while? ??) // ??, ?? setter ? set.(? ?? ? String set.) // While? ??, File? ?? (text...)? ? value? . // ?? setter ? set. // DB? .(File ? DB? . File ? ftp ? .) // // //=========================================================================================================== MultipartRequest mpr = new MultipartRequest(request, fileDir1, max, "UTF-8", new DefaultFileRenamePolicy()); Enumeration formNames = mpr.getFileNames(); //ArrayList<File> fileList = new ArrayList<File>(); //HashMap<String, File> fileMap = new HashMap<String, File>(); while (formNames.hasMoreElements()) { System.out.println("============================"); fileInput = (String) formNames.nextElement(); System.out.println("fileInput : " + fileInput); //Console ?. ==> fileInput : imgSmall1. fileName = mpr.getFilesystemName(fileInput); System.out.println("fileName : " + fileName); //Console ?. ==> fileName : IMG_1200.png System.out.println("============================"); if (fileName != null) { System.out.println("if ?"); switch (fileInput) { case "imgSmall1": //fileDir2 = fileDir1 + "/main"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgSmall1("main/" + fileName); System.out.println("restaurant.getImageSmall1 : " + restaurant.getImgSmall1()); //Console ?. ==> fileDirectory : C:\(?)\wtpwebapps\TastyRoad05\resources/img/ //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgSmall1", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); //Console ?. ==>C:\(?)\wtpwebapps\TastyRoad05\resources/img/(??).png break; case "imgBig1": //fileDir2 = fileDir1 + "/detail"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgBig1("detail/" + fileName); System.out.println("restaurant.getImageBig1 : " + restaurant.getImgBig1()); //Console ?. ==> fileDirectory : C:\(?)\wtpwebapps\TastyRoad05\resources/img/ //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgBig1", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); //Console ?. ==>C:\(?)\wtpwebapps\TastyRoad05\resources/img/(??).png break; case "imgBig2": //fileDir2 = fileDir1 + "/detail"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgBig2("detail/" + fileName); System.out.println("restaurant.getImageBig2 : " + restaurant.getImgBig2()); //Console ?. ==> fileDirectory : C:\(?)\wtpwebapps\TastyRoad05\resources/img/ //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgBig2", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); //Console ?. ==>C:\(?)\wtpwebapps\TastyRoad05\resources/img/(??).png break; case "imgBig3": //fileDir2 = fileDir1 + "/detail"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgBig3("detail/" + fileName); System.out.println("restaurant.getImageBig3 : " + restaurant.getImgBig3()); //Console ?. ==> fileDirectory : C:\(?)\wtpwebapps\TastyRoad05\resources/img/ //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgBig3", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); //Console ?. ==>C:\(?)\wtpwebapps\TastyRoad05\resources/img/(??).png break; case "imgMenu": //fileDir2 = fileDir1 + "/menu"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgMenu("menu/" + fileName); System.out.println("restaurant.getImageMenu : " + restaurant.getImgMenu()); //Console ?. ==> fileDirectory : C:\(?)\wtpwebapps\TastyRoad05\resources/img/ //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgMenu", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); //Console ?. ==>C:\(?)\wtpwebapps\TastyRoad05\resources/img/(??).png break; }//endOfSwith } //endOfIf //type = "file" ? ? value? set. restaurant.setName(mpr.getParameter("name")); restaurant.setAddr(mpr.getParameter("addr")); restaurant.setPhone(mpr.getParameter("phone")); restaurant.setLicenseNo(mpr.getParameter("licenseNo")); restaurant.setGeoLat(Double.parseDouble(mpr.getParameter("geoLat"))); restaurant.setGeoLong(Double.parseDouble(mpr.getParameter("geoLong"))); restaurant.setCopyComment(mpr.getParameter("copyComment")); restaurant.setResCategory(Integer.parseInt(mpr.getParameter("resCategory"))); } //endOfWhile //=============================================<FTP -INSERT>======================================== // // +---------------------+ // | author : . | // +---------------------+ // | Date : 2014-07-02 | // +---------------------+ // // 1. FTP ? ? ? ?? ? . // 2. ?? ? ? , ? ?. // 3. FTP . // FTPTransfer inner ? , insert . // //inner class // public FTPTransfer{ // insert(SERVER_IP, PORT, ID, PASSWORD, UPLOAD_DIR, Makedir, map) // } // // FTPTransfer ? ? ?. // FTPTransfer transfer = new FTPTransfer(); // // insert , // MultipartRequest File ? <String, File> map? . // transfer.insert(SERVER_IP, PORT, ID, PASSWORD, UPLOAD_DIR, null, fileMap); // // insert ? for map? value(File ?) , ftp.storeFile()? , // ftp . // // ftp ? ??, ? ? ? ?? . // // //=========================================================================================================== FTPTransfer transfer = new FTPTransfer(); System.out.println("==============================ftp ======================================="); boolean result = transfer.insert(SERVER_IP, PORT, ID, PASSWORD, UPLOAD_DIR, null, fileMap); // transfer.FtpPut(SERVER_IP, PORT, ID, PASSWORD, UPLOAD_DIR, null, fileList); System.out.println("==============================ftp ======================================="); // for(int i=0; i<fileList.size();i++){ // System.out.println("Local File for ......"); // File file1 = new File(fileList.get(i).toString()); // file1.delete(); // // if(file1.exists()){ // System.out.println(" Failed......"); // } // else{ // System.out.println(" Ok!!!!!!"); // } // }//endOfForStatement //ftp ? ?, ? ? ? ? ?? . if (result == true) { set = fileMap.keySet(); //set.toString(); //Console ?. ==> [imgSmall1, imgBig1, imgBig2, imgBig3, keys = set.toString().subSequence(1, set.toString().length() - 1).toString().split(", "); for (int i = 0; i < keys.length; i++) { System.out.println("Local File for ......"); File file1 = new File(fileMap.get(keys[i]).toString()); file1.delete(); if (file1.exists()) { System.out.println(" Failed......"); } else { System.out.println(" Ok!!!!!!"); } } //endOfForStatement } // if(mpr.getFileNames().equals("imgSmall1")){ // filePath=dir+"/main/"; // imageFileLists.add(mpr.getFile("imgSmall1")); // imageFileName.add(mpr.getOriginalFileName("imgSmall1")); // System.out.println(mpr.getFile("imgSmall1")); // System.out.println(mpr.getOriaginalFileName("imgSmall1")); // // } // // else if(mpr.getFileNames().equals("imgBig1")){ // filePath=dir+"/detail/"; // imageFileLists.add(mpr.getFile("imgBig1")); // imageFileName.add(mpr.getOriginalFileName("imgBig1")); // System.out.println(mpr.getFile("imgBig1")); // System.out.println(mpr.getOriginalFileName("imgBig1")); // } // // else if(mpr.getFileNames().equals("imgBig2")){ // filePath=dir+"/detail/"; // imageFileLists.add(mpr.getFile("imgBig2")); // imageFileName.add(mpr.getOriginalFileName("imgBig2")); // System.out.println(mpr.getFile("imgBig2")); // System.out.println(mpr.getOriginalFileName("imgBig2")); // } // // else if(mpr.getFileNames().equals("imgBig3")){ // filePath=dir+"/detail/"; // imageFileLists.add(mpr.getFile("imgBig3")); // imageFileName.add(mpr.getOriginalFileName("imgBig3")); // System.out.println(mpr.getFile("imgBig3")); // System.out.println(mpr.getOriginalFileName("imgBig3")); // } // // else if(mpr.getFileNames().equals("imgMenu")){ // filePath=dir+"/menu/"; // imageFileLists.add(mpr.getFile("imgMenu")); // imageFileName.add(mpr.getOriginalFileName("imgMenu")); // System.out.println(mpr.getFile("imgMenu")); // System.out.println(mpr.getOriginalFileName("imgMenu")); // } // // for(int i=0; i<imageFileName.size();i++){ // fileName = imageFileName.get(i); // System.out.println(fileName); // // if(!fileName.equals("")){ // String fullFileName = filePath + fileName; // } // } // /* by .*/ // try { // //? String File? Parsing MultipartParser ? ? // //10 * 1024 * 1024 = 10MB. ? ? // MultipartParser mp = new MultipartParser(request, 10 * 1024 * 1024); // //UTF-8? . ? ? // mp.setEncoding("UTF-8"); // //A Part is an abstract upload part which represents an INPUT form element // //in a multipart/form-data form submission // //Part ?/. Part form tag upload? part()? ?. file String? // Part part = null; // //A ParamPart is an upload part which represents // //a normal INPUT (for example a non TYPE="file") form parameter. // //ParamPart ? ?/. ParamPart Server ? file? parameter // //, name="", addr="?" ,.. ??? ? ParamPart // ParamPart paramPart = null; // //? parameter? ?. , name, addr, imgSmall1, imgBig1 // String paramName = ""; // //? parameter ? ? . , , ? // String paramValue = null; // //File ? ?/. ? ?? ? ? // File file = null; // //? ?? ? // String fileName = ""; // //dynamic ? ? // String baseFilePath = request.getRealPath("/")+"resources/img"; // //System.out.println(baseFilePath); // // //upload? Part? // while ((part = (Part) mp.readNextPart()) != null) { // // paramName = part.getName(); // //? part? ? ???, // if (part.isFile()) { // //upload? ?? ? // String filePath = null; // //Part? FilePart(?) () // FilePart filePart = (FilePart) part; // //FilePart ? ? // fileName = (String) filePart.getFileName(); // // +?? ? StringBuffer type? fileRef ? // StringBuffer fileRef = new StringBuffer(); // // //parameter? ?? ? ?? ? set // switch(paramName){ // case "imgSmall1" : // filePath = baseFilePath + "/main"; // break; // case "imgBig1" : // case "imgBig2" : // case "imgBig3" : // filePath = baseFilePath + "/detail"; // break; // case "imgMenu" : // filePath = baseFilePath + "/menu"; // break; // }//end of switch // // // ? ?(). dynamic // filePart.writeTo(new File(String.valueOf(fileRef.append(filePath).append(File.separator).append(fileName)))); // //fileName ?? ?? , ?? ? ? // //DB? null? ? // if(fileName != null){ // //restaurant ?? ? ? ? set(String). ?? ! // //? ? resources/img ? ? ?. ? // //, imgSmall1? resources/img/main/?? // // ?? main/?? ? DB? ? // switch(paramName){ // case "imgSmall1" : // restaurant.setImgSmall1("main/" +fileName); // break; // case "imgBig1" : // restaurant.setImgBig1("detail/" +fileName); // break; // case "imgBig2" : // restaurant.setImgBig2("detail/" +fileName); // break; // case "imgBig3" : // restaurant.setImgBig3("detail/" +fileName); // break; // case "imgMenu" : // restaurant.setImgMenu("menu/" +fileName); // break; // }//end of switch // }//end of if // //text ? parameter? - , file? . // }else { // //ParamPart parameter // paramPart = (ParamPart)part; // //parameter? ? String // paramValue = new String(paramPart.getStringValue()); // //paramValue ? restaurant? set // if(paramValue != null){ // //parameter ?? ? switch restaurant ?? set // // ? DB? // switch(paramName){ // case "name" : // restaurant.setName(paramValue); // break; // case "addr" : // restaurant.setAddr(paramValue); // break; // case "phone" : // restaurant.setPhone(paramValue); // break; // case "licenseNo" : // restaurant.setLicenseNo(paramValue); // break; // case "geoLat" : // restaurant.setGeoLat(Double.parseDouble(paramValue)); // break; // case "geoLong" : // restaurant.setGeoLong(Double.parseDouble(paramValue)); // break; // case "copyComment" : // restaurant.setCopyComment(paramValue); // break; // case "resCategory" : // restaurant.setResCategory(Integer.parseInt(paramValue)); // break; // }//end of switch // }//end of if // }//end of if-else // }//end of while // } catch (Exception e){ // e.printStackTrace(); // throw e; // }//end of catch // - DB? upload? ?? restaurantService.addRestaurant(restaurant); // ? ? ?? ?? //return "forward:/restaurant/listRestaurant"; //CurrentPage 1 set Search search = new Search(); search.setCurrentPage(1); search.setPageSize(pageSize); //field? ? pageSize set search.setPageSize(pageSize); //DB? (? null)? ? restaurant ? List restaurantList = restaurantService.getRestaurantList(search); //Page instance ?(?? page navigation ) Page resultPage = new Page(search.getCurrentPage(), restaurantService.getTotalCount(search), pageUnit, pageSize); //restaurant list console? for (int i = 0; i < restaurantList.size(); i++) { restaurant = (Restaurant) restaurantList.get(i); System.out.println("restaurant.toString()" + restaurant.toString() + "__"); } //response? model view ? ModelAndView ? ? ModelAndView modelAndView = new ModelAndView(); // view set. //WEB-INF/restaurantViews/restaurantList.jsp (servlet-context.xml) modelAndView.setViewName("restaurantViews/restaurantList"); //? 3??. ?,? response? modelAndView.addObject("restaurantList", restaurantList); modelAndView.addObject("resultPage", resultPage); modelAndView.addObject("search", search); return modelAndView; } @RequestMapping(value = "/getRestaurantDetail", method = RequestMethod.POST) public ModelAndView getRestaurantDetailAction(@ModelAttribute("restaurant") Restaurant restaurant) throws Exception { System.out.println("_______________________________________________"); System.out.println("==> /restaurant/getRestaurantDetail__call !!!"); System.out.println("_______________________________________________"); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("restaurantViews/restaurantDetail"); modelAndView.addObject("restaurant", restaurantService.getRestaurant(restaurant.getResId())); return modelAndView; } //==============================================<FTP -UPDATE>========================================= // // +---------------------+ // | author : . | // +---------------------+ // | Date : 2014-07-02 | // +---------------------+ // // 1. ? ? MultipartRequest . // 2. restaurantUpdate.jsp? file? MultipartRequest . // 3. restaurantUpdate.jsp ? 2. // form enctype="multipart/form-data" ? . // ? input ? type="file"? ? . // input ? hidden value resId. // 4. . // ftp? ? ? ?? delete. // ? ? parameter value? ?? ? Restaurant? set , DB update . // db? ? ? ? ? ftp? . // // //=========================================================================================================== @RequestMapping(value = "/getUpdateRestaurantView", method = RequestMethod.POST) public ModelAndView getUpdateRestaurantViewAction(@ModelAttribute("restaurant") Restaurant restaurant) throws Exception { System.out.println("_______________________________________________"); System.out.println("==> /restaurant/getUpdateRestaurantView__call !!!"); System.out.println("_______________________________________________"); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("restaurantViews/restaurantUpdate"); modelAndView.addObject("restaurant", restaurantService.getRestaurant(restaurant.getResId())); return modelAndView; } @RequestMapping(value = "/updateRestaurant", method = RequestMethod.POST) public ModelAndView updateRestaurantAction(@ModelAttribute("restaurant") Restaurant restaurant, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("_______________________________________________"); System.out.println("==> /restaurant/updateRestaurant__call !!!"); System.out.println("_______________________________________________"); // CommonsMultipartResolver cmr = new CommonsMultipartResolver(); // String[] s1 = (String[])cmr.resolveMultipart(request).getParameterMap().get("resId"); // restaurant.setResId(Integer.parseInt(s1[0])); String fileDir1 = request.getRealPath("/") + "resources/img"; int max = 5 * 640 * 480; MultipartRequest mpr = new MultipartRequest(request, fileDir1, max, "UTF-8", new DefaultFileRenamePolicy()); restaurant.setResId(Integer.parseInt(mpr.getParameter("resId"))); System.out.println(restaurant.getResId()); System.out.println(restaurant); FTPTransfer transfer = new FTPTransfer(); transfer.delete(SERVER_IP, PORT, ID, PASSWORD, UPLOAD_DIR, restaurantService.getRestaurant(restaurant.getResId())); Enumeration formNames = mpr.getFileNames(); String fileInput = ""; String fileName = ""; String fileDir2 = ""; //ArrayList<File> fileList = new ArrayList<File>(); //HashMap<String, File> fileMap = new HashMap<String, File>(); while (formNames.hasMoreElements()) { System.out.println("============================"); fileInput = (String) formNames.nextElement(); System.out.println("fileInput : " + fileInput); fileName = mpr.getFilesystemName(fileInput); System.out.println("fileName : " + fileName); System.out.println("============================"); if (fileName != null) { System.out.println("if ?"); switch (fileInput) { case "imgSmall1": fileDir2 = fileDir1 + "/main"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgSmall1("main/" + fileName); System.out.println("restaurant.getImageSmall1 : " + restaurant.getImgSmall1()); //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgSmall1", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); break; case "imgBig1": fileDir2 = fileDir1 + "/detail"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgBig1("detail/" + fileName); System.out.println("restaurant.getImageBig1 : " + restaurant.getImgBig1()); //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgBig1", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); break; case "imgBig2": fileDir2 = fileDir1 + "/detail"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgBig2("detail/" + fileName); System.out.println("restaurant.getImageBig2 : " + restaurant.getImgBig2()); //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgBig2", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); break; case "imgBig3": fileDir2 = fileDir1 + "/detail"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgBig3("detail/" + fileName); System.out.println("restaurant.getImageBig3 : " + restaurant.getImgBig3()); //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgBig3", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); break; case "imgMenu": fileDir2 = fileDir1 + "/menu"; System.out.println("fileDirectory : " + fileDir2); restaurant.setImgMenu("menu/" + fileName); System.out.println("restaurant.getImageMenu : " + restaurant.getImgMenu()); //fileList.add(mpr.getFile(fileInput)); fileMap.put("imgMenu", mpr.getFile(fileInput)); //System.out.println("getFile? ==> " + mpr.getFile(fileInput)); break; }//endOfSwith } //endOfIf restaurant.setName(mpr.getParameter("name")); restaurant.setAddr(mpr.getParameter("addr")); restaurant.setPhone(mpr.getParameter("phone")); restaurant.setLicenseNo(mpr.getParameter("licenseNo")); restaurant.setGeoLat(Double.parseDouble(mpr.getParameter("geoLat"))); restaurant.setGeoLong(Double.parseDouble(mpr.getParameter("geoLong"))); restaurant.setCopyComment(mpr.getParameter("copyComment")); restaurant.setResCategory(Integer.parseInt(mpr.getParameter("resCategory"))); } //endOfWhile restaurantService.updateRestaurant(restaurant); boolean result = transfer.insert(SERVER_IP, PORT, ID, PASSWORD, UPLOAD_DIR, null, fileMap); //ftp ? ?, ? ? ? ? ?? . if (result == true) { set = fileMap.keySet(); //set.toString(); //Console ?. ==> [imgSmall1, imgBig1, imgBig2, imgBig3, keys = set.toString().subSequence(1, set.toString().length() - 1).toString().split(", "); for (int i = 0; i < keys.length; i++) { System.out.println("Local File for ......"); File file1 = new File(fileMap.get(keys[i]).toString()); file1.delete(); if (file1.exists()) { System.out.println(" Failed......"); } else { System.out.println(" Ok!!!!!!"); } } //endOfForStatement } ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("restaurantViews/restaurantDetail"); modelAndView.addObject("restaurant", restaurantService.getRestaurant(restaurant.getResId())); return modelAndView; } @RequestMapping(value = "/removeRestaurant", method = RequestMethod.POST) //?? web client? ? restaurant? ? public ModelAndView removeRestaurantAction(@ModelAttribute("restaurant") Restaurant restaurant) throws Exception { System.out.println("_______________________________________________"); System.out.println("==> /restaurant/removeRestaurant__call !!!"); System.out.println("_______________________________________________"); //=============================================<FTP >======================================== FTPTransfer transfer = new FTPTransfer(); transfer.delete(SERVER_IP, PORT, ID, PASSWORD, UPLOAD_DIR, restaurantService.getRestaurant(restaurant.getResId())); restaurantService.removeRestaurant(restaurant.getResId()); //CurrentPage 1 set Search search = new Search(); search.setCurrentPage(1); search.setPageSize(pageSize); //field? ? pageSize set search.setPageSize(pageSize); //DB? (? null)? ? restaurant ? List restaurantList = restaurantService.getRestaurantList(search); //Page instance ?(?? page navigation ) Page resultPage = new Page(search.getCurrentPage(), restaurantService.getTotalCount(search), pageUnit, pageSize); //restaurant list console? for (int i = 0; i < restaurantList.size(); i++) { restaurant = (Restaurant) restaurantList.get(i); System.out.println("restaurant.toString()" + restaurant.toString() + "__"); } //response? model view ? ModelAndView ? ? ModelAndView modelAndView = new ModelAndView(); // view set. //WEB-INF/restaurantViews/restaurantList.jsp (servlet-context.xml) modelAndView.setViewName("restaurantViews/restaurantList"); //? 3??. ?,? response? modelAndView.addObject("restaurantList", restaurantList); modelAndView.addObject("resultPage", resultPage); modelAndView.addObject("search", search); return modelAndView; } //=============================================<ftp ? >=============================================== // // +---------------------+ // | author : . | // +---------------------+ // | Date : 2014-07-02 | // +---------------------+ // // <insert> // 1. FTPClient ? ?. // 2. ftp connect ?. disconnect. // 3. connection? ?, login ?. login logout. // 4. File ? map? value for , new File() ?? ?? . // 5. FileInputStream? , ftp.storeFile() File ? . // // <delete> // 1. insert 1~3 ??. // 2. restaurant ? getter HashMap . // 3. for ? ? map? value . // 4. ?? ftp . // 5. ftp.delete() ? ?? . // //=============================================================================================================== class FTPTransfer { //Field FTPClient ftp = null; int reply = 0; boolean result = false; boolean isSuccess = false; File uploadFile = null; FileInputStream fis = null; String fileToDelete = null; //? ?? . public boolean insert(String ip, int port, String id, String password, String uploaddir, String makedir, HashMap<String, File> fileMap) { try { ftp = new FTPClient(); ftp.connect(ip, port); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } if (!ftp.login(id, password)) { ftp.logout(); return result; } ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); set = fileMap.keySet(); //set.toString(); //Console ?. ==> [imgSmall1, imgBig1, imgBig2, imgBig3, keys = set.toString().subSequence(1, set.toString().length() - 1).toString().split(", "); System.out.println("=======ftp initial working directory========="); System.out.println(ftp.printWorkingDirectory());//ftp print. //Console ?. ==> / System.out.println("============================================="); for (int i = 0; i < keys.length; i++) { System.out.println("Parsing? for ..."); String sourceFile = fileMap.get(keys[i]).toString(); //+? String keyFileName = keys[i]; System.out.println("sourceFile ==> " + sourceFile); //Console ?. ==> sourceFile ==> C:\(?)\wtpwebapps\TastyRoad05\resources\img\IMG_1205.png // System.out.println("=======ftp initial working directory========="); // System.out.println(ftp.printWorkingDirectory()); // ftp.changeWorkingDirectory("/"+uploaddir); // ftp.changeWorkingDirectory("/teamdwf"); // System.out.println("============================================="); // // switch(keyFileName){ // case "imgSmall1": // System.out.println("=======ftp imgSmall1 current working directory========="); // System.out.println("[imgSmall1] switch-case ......"); // ftp.changeWorkingDirectory("main"); // System.out.println(ftp.printWorkingDirectory()); // System.out.println("============================================="); // // break; // case "imgBig1": // case "imgBig2": // case "imgBig3": // System.out.println("=======ftp imgBig1/Big2/Big3 current working directory========="); // System.out.println("[imgBig1, imgBig2, imgBig3] switch-case ......"); // ftp.changeWorkingDirectory("detail"); // System.out.println(ftp.printWorkingDirectory()); // System.out.println("============================================="); // // break; // case "imgMenu": // System.out.println("=======ftp imgMenu current working directory========="); // System.out.println("[imgMenu] switch-case ......"); // ftp.changeWorkingDirectory("menu"); // System.out.println(ftp.printWorkingDirectory()); // System.out.println("============================================="); // break; // default: // System.out.println("============================================="); // System.out.println("[Default] switch-case ......"); // ftp.changeWorkingDirectory(uploaddir); // System.out.println(ftp.printWorkingDirectory()); // System.out.println("============================================="); // break; // } if (keyFileName.equals("imgSmall1")) { //ftp.makeDirectory("main"); System.out.println("=======ftp imgSmall1 current working directory========="); System.out.println("[imgSmall1] switch-case ......"); ftp.changeWorkingDirectory("/" + uploaddir);// . ftp.changeWorkingDirectory("/teamdwf/main");// . //Console ?. ==> /teamdwf/menu //ftp.changeWorkingDirectory("/main"); System.out.println(ftp.printWorkingDirectory()); System.out.println("============================================="); } else if (keyFileName.equals("imgBig1") || keyFileName.equals("imgBig2") || keyFileName.equals("imgBig3")) { //ftp.makeDirectory("detail"); System.out.println("=======ftp imgBig1/Big2/Big3 current working directory========="); System.out.println("[imgBig1, imgBig2, imgBig3] switch-case ......"); ftp.changeWorkingDirectory("/" + uploaddir);// . ftp.changeWorkingDirectory("/teamdwf/detail");// . //Console ?. ==> /teamdwf/detail //ftp.changeWorkingDirectory("/detail"); System.out.println(ftp.printWorkingDirectory()); System.out.println("============================================="); } else if (keyFileName.equals("imgMenu")) { //ftp.makeDirectory("menu"); System.out.println("=======ftp imgMenu current working directory========="); System.out.println("[imgMenu] switch-case ......"); ftp.changeWorkingDirectory("/" + uploaddir);// . ftp.changeWorkingDirectory("/teamdwf/menu");// . //Console ?. ==> /teamdwf/menu //ftp.changeWorkingDirectory("/menu"); System.out.println(ftp.printWorkingDirectory()); System.out.println("============================================="); } uploadFile = new File(sourceFile);//ftp ? new File()? ?. try { fis = new FileInputStream(uploadFile); isSuccess = ftp.storeFile(uploadFile.getName(), fis);//File ? ftp . if (isSuccess) { System.out.println(sourceFile + " ? FTP "); } // System.out.println("=======ftp initial working directory========="); // System.out.println(ftp.printWorkingDirectory()); // ftp.changeWorkingDirectory("/"+uploaddir); // ftp.changeWorkingDirectory("/teamdwf"); // System.out.println("============================================="); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } System.out.println("Parsing? for ......"); ftp.logout(); result = true; } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (ftp != null && ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { } } } return result; } public boolean delete(String ip, int port, String id, String password, String deletedir, Restaurant restaurant) throws Exception { System.out.println("delete ."); ftp = new FTPClient(); ftp.connect(ip, port); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { System.out.println("Connection Failed......"); ftp.disconnect(); return result; } if (!ftp.login(id, password)) { System.out.println("Could not login to server......"); return result; } System.out.println("restaurant.getImgSmall1() ==> " + restaurant.getImgSmall1()); System.out.println("restaurant.getImgBig1() ==> " + restaurant.getImgBig1()); System.out.println("restaurant.getImgBig2() ==> " + restaurant.getImgBig2()); System.out.println("restaurant.getImgBig3() ==> " + restaurant.getImgBig3()); System.out.println("restaurant.getImgMenu() ==> " + restaurant.getImgMenu()); HashMap<String, String> imgPathMapping = new HashMap<String, String>(); imgPathMapping.put("imgSmall1", restaurant.getImgSmall1()); imgPathMapping.put("imgBig1", restaurant.getImgBig1()); imgPathMapping.put("imgBig2", restaurant.getImgBig2()); imgPathMapping.put("imgBig3", restaurant.getImgBig3()); imgPathMapping.put("imgMenu", restaurant.getImgMenu()); System.out.println(imgPathMapping); set = imgPathMapping.keySet(); System.out.println(set.toString()); keys = set.toString().subSequence(1, set.toString().length() - 1).toString().split(", "); for (int i = 0; i < imgPathMapping.size(); i++) { System.out.println("FTP File for ......"); if (keys[i].equals("imgSmall1")) { fileToDelete = "/" + deletedir + "/" + imgPathMapping.get(keys[i]); result = ftp.deleteFile(fileToDelete); if (result) { System.out.println("Delete file from imageserver success......"); } else { System.out.println("Could not delete file from imageserver......"); return result; } } else if (keys[i].equals("imgBig1") || keys[i].equals("imgBig2") || keys[i].equals("imgBig3")) { fileToDelete = "/" + deletedir + "/" + imgPathMapping.get(keys[i]); result = ftp.deleteFile(fileToDelete); if (result) { System.out.println("Delete file from imageserver success......"); } else { System.out.println("Could not delete file from imageserver......"); return result; } } else if (keys[i].equals("imgMenu")) { fileToDelete = "/" + deletedir + "/" + imgPathMapping.get(keys[i]); result = ftp.deleteFile(fileToDelete); if (result) { System.out.println("Delete file from imageserver success......"); } else { System.out.println("Could not delete file from imageserver......"); return result; } } //end Of IfStatement } //end Of ForStatement return result; }//end Of Method }//end Of InnerClass }//end Of Class