Spring + FTP 文件上傳下載

首先說一下ftp的特色是:傳輸速度快,適用於上傳大文件,適用於局域網絡。 java

直奔主題,這裏採用apache提供的網絡包,commons-net.jar。我用的3.3。 spring

ftp上傳須要ftp服務器。這裏描述客戶端的實現。 apache

spring 文件上傳這一塊就不作描述 服務器

直接代碼(上傳) 網絡


@RequestMapping(value = "/uploadConfigFile")
	@ResponseBody
	public void uploadConfigFile(@RequestParam("uploadFile") MultipartFile uploadFile){
		FTPClient ftpClient = new FTPClient(); 
		try { 
			ftpClient.connect("120.120.120.156",21); 
			ftpClient.login("user1", "user1"); 
			ftpClient.enterLocalPassiveMode();
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
			
			//設置上傳目錄 
			ftpClient.changeWorkingDirectory("/GOS_CAS/RECONVERT/cas_config_reconvert"); 
			String fileName = new String(uploadFile.getOriginalFilename().getBytes("utf-8"),"iso-8859-1");
			
			FTPFile[] fs = ftpClient.listFiles();  
			if (fs!=null && fs.length>0) {
				for(int i=0;i<fs.length;i++){
					if (fs[i].getName().equals(fileName)) {
						ftpClient.deleteFile(fs[i].getName());
						break;
					}
				}
			}
			OutputStream os = ftpClient.appendFileStream(fileName);
			byte[] bytes = new byte[1024];
			InputStream is = uploadFile.getInputStream();
			// 開始複製 其實net已經提供了上傳的函數,可是我想多是我這個版本有點問題                                                           //ftpClient.storeFile("", fis); 
                                            // 因而我本身write出去了,其實我想都是同樣的效果,在這裏告訴你們這兩種方式都能實現 
                         int c;
			// 暫未考慮中途終止的狀況
			while ((c = is.read(bytes)) != -1) {
				os.write(bytes, 0, c);
			}
			os.flush();
			is.close();
			os.close();
			RestoreConfiguration restoreConfiguration = new RestoreConfiguration();
			restoreConfiguration.setStrName(fileName);
			getServiceStub().restoreConfiguration(restoreConfiguration);
		} catch (IOException e) { 
			e.printStackTrace(); 
		} finally { 
			try { 
				ftpClient.disconnect(); 
			} catch (IOException e) { 
				e.printStackTrace(); 
			} 
		}
	}

下載 app

@RequestMapping(value = "/downloadConfigFile")
	public void downloadConfigFile(HttpServletResponse response,@RequestParam("fileName")String fileName) {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("multipart/form-data");
		
			FTPClient ftpClient = new FTPClient(); 
		    try {  
		        int reply;  
		        ftpClient.connect("120.120.120.156",21); 
	            ftpClient.login("user1", "user1");
		        reply = ftpClient.getReplyCode();  
		        if (!FTPReply.isPositiveCompletion(reply)) {  
		        	ftpClient.disconnect();
		            return;  
		        }
		        ftpClient.changeWorkingDirectory("/GOS_CAS/BACKUP/cas_config_backup");//轉移到FTP服務器目錄  
		        FTPFile[] fs = ftpClient.listFiles();  
		        for(int i=0;i<fs.length;i++){  
		            if(fs[i].getName().equals(fileName)){
		            	String saveAsFileName = new String(fs[i].getName().getBytes("UTF-8"), "ISO8859-1");  
		    			response.setHeader("Content-Disposition", "attachment;fileName="+saveAsFileName);
		    			OutputStream os = response.getOutputStream();
		                ftpClient.retrieveFile(fs[i].getName(), os);
		                os.flush();
		                os.close();
		                break;
		            }
		        }
		        ftpClient.logout();  
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    } finally {  
		        if (ftpClient.isConnected()) {  
		            try {  
		                ftpClient.disconnect();  
		            } catch (IOException ioe) {  
		            }  
		        }  
		    }  
	}
以上就簡單的本身總結了一下,但願對各位有點幫助,同時不足之處但願大神斧正
相關文章
相關標籤/搜索