package cn.ksource.web.controller.business.component; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.commons.CommonsMultipartFile; import cn.ksource.beans.GG_FILES; import cn.ksource.core.util.ConvertUtil; import cn.ksource.core.util.DownloadUtil; import cn.ksource.core.util.FtpClient; import cn.ksource.core.util.JsonUtil; import cn.ksource.core.util.UploadStatus; import cn.ksource.core.web.WebUtil; import cn.ksource.web.service.file.FileService; @SuppressWarnings("rawtypes") @Controller @RequestMapping("/platform/fileUpload") public class FileUploadController { private FileService fileService; /** * 文件上传 * * @author chenlong * @return */ @RequestMapping(value = "/uploadIndex", method = RequestMethod.GET) public String uploadFile() { return "fileUpload/uploadIndex"; } @RequestMapping(value = "/Upload2Ftp.html", method = RequestMethod.POST) public void doUploadFile(@RequestParam(value="file") CommonsMultipartFile file, HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException { Map res = new HashMap(); if (file != null) { String fileName = file.getOriginalFilename(); long fileSize = file.getSize(); String path = "/cl/doc/"; String remote = path + fileName; String ext = fileName.substring(fileName.lastIndexOf(".") + 1).trim().toLowerCase(); InputStream is = file.getInputStream(); FtpClient ftpClient = new FtpClient(); ftpClient.connect(); UploadStatus status = ftpClient.upload(is, remote); System.out.println("-----------"+status); ftpClient.disconnect(); //上传到图片服务器 //file.transferTo(new File("e:/"+fileName)); res.put("fileName", fileName); res.put("path", remote); res.put("fileSize", ConvertUtil.obj2StrBlank(fileSize)); res.put("ext", ext); } WebUtil.write(response, JsonUtil.map2Json(res)); } @RequestMapping(value = "/downFtpFile.html") public void downFtpFile(HttpServletRequest request,HttpServletResponse response){ String fileId = request.getParameter("fileId"); GG_FILES file = new GG_FILES(fileId).getInstanceById(); String remote = file.getFile_path(); FtpClient ftpClient = new FtpClient(); try { ftpClient.connect(); InputStream in = ftpClient.download(remote); ftpClient.disconnect(); DownloadUtil.download(response, in, file.getFile_name() ); } catch (IOException e) { e.printStackTrace(); } } }