ファイル転送ftpファイルダウンロード
2086 ワード
第2部:ftpファイルダウンロード
@Controller
public class FileTransferController extends BaseController{
//
public String tempPath = FileUtil.getProjectPath()+"\\temp\\";
/**
*
* @author zheng_liming
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/fileTransfer/download", method = RequestMethod.POST)
public void download(HttpServletRequest request,HttpServletResponse response) throws Exception{
DynamicForm dynamicForm =(DynamicForm) PlatformContext.getRequestAttribute(SystemConstants.DYNAMICFORM);
String fileName = dynamicForm.getString("fileName");
String filePath = dynamicForm.getString("filePath");
File file = new File(tempPath);
if (!file.exists()) {
file.mkdirs();
}
// ftp
FtpClient ftpClient = FtpUtil.connectServer(filePath);
// ftp
boolean downResult = FileUtil.ftpDownload(ftpClient, fileName, tempPath+fileName);
if(downResult){
//
downFile(tempPath,fileName);
}
//
FileUtil.removeAllFile(tempPath);
FileUtil.removeFolder(tempPath);
}
/**
*
* @author zheng_liming
* @param filePath
* @param fileName
*/
public void downFile(String filePath,String fileName) throws Exception{
FileInputStream fis = new FileInputStream(new File(filePath+fileName));
//
HttpServletResponse response = getResponse();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
//
int b = 0;
PrintWriter out = response.getWriter();
while((b=fis.read())!=-1) {
out.write(b);
}
//
fis.close();
out.close();
}
}