Java tutorial
/* * * Copyright SHMsoft, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.freeeed.ep.web.controller; import java.io.File; import org.apache.log4j.Logger; import org.freeeed.ep.processing.ProcessingResult; import org.freeeed.ep.processing.ProcessingStatus; import org.freeeed.ep.processing.Processor; import org.freeeed.ep.processing.nsf.NSFTask; import org.freeeed.ep.web.BaseController; import org.freeeed.ep.web.WebConstants; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.ModelAndView; import com.google.gson.Gson; /** * * Class ProcessingController. * * @author ilazarov * */ public class ProcessingController extends BaseController { private static final Logger log = Logger.getLogger(ProcessingController.class); private Processor processor; private String uploadDir; private String workDir; public ModelAndView execute() { log.info("request received... "); if (!(request instanceof MultipartHttpServletRequest)) { valueStack.put("status", "error"); return new ModelAndView(WebConstants.PROCESS_REQUEST_RESPONSE); } MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("file"); String taskId = multipartRequest.getParameter("taskId"); ProcessingResult result = null; try { File uploadDirFile = new File(uploadDir); uploadDirFile.mkdirs(); String fileName = uploadDir + File.separator + file.getOriginalFilename(); File destination = new File(fileName); log.info("File transfer started... "); file.transferTo(destination); NSFTask task = new NSFTask(destination.getAbsolutePath(), workDir); result = processor.submitTask(taskId, task); } catch (Exception e) { log.error("Problem uploading file: ", e); result = new ProcessingResult(); result.setStatus(ProcessingStatus.ERROR); } Gson gson = new Gson(); String data = gson.toJson(result); valueStack.put("status", data); log.info("File submitted for processing!"); return new ModelAndView(WebConstants.PROCESS_REQUEST_RESPONSE); } public void setProcessor(Processor processor) { this.processor = processor; } public void setUploadDir(String uploadDir) { this.uploadDir = uploadDir; } public void setWorkDir(String workDir) { this.workDir = workDir; } }