标签:
导入包Ftp包 commons-net-3.3.jar
直接例子:
Ftp工具类
package com.book.utils; import java.io.IOException; import java.io.InputStream; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class ftpUtil { // ("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", innput) public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(url, port);//连接FTP服务器 //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 ftp.login(username, password);//登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.changeWorkingDirectory(path); ftp.storeFile(filename, input); input.close(); ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; } }
页面代码:
<body> <form action="shangc.do" id="soft_add" name="data_add" method="post" enctype="multipart/form-data"> <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top: 20px;margin-bottom: 15px;"> <tr><td align="center"><font size="3"><b> 数 据 文 件 入 库 </b></font></td></tr> <tr><td align="center"><FONT SIZE="" COLOR="red">*</FONT> <font size="2">为必填项</font></td></tr> </table> <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" class="borderquan"> <tr> <td width="20%" height="50" align="center" class="borderbottomright">数据文件</td> <td width="80%" class="borderbottom"><label> <input type="file" name="dataFile" id="dataFile" style="width: 310px;" onblur="checkDataFile();"/> <FONT SIZE="" COLOR="red" style="font-size: 12px">* </FONT> <span id="staError" style="font-size: 9pt; font-family: 黑体; color: red"></span> <FONT SIZE="" COLOR="red" style="font-size: 12px"><br />文件名格式如:2_v110.xml 下划线前是基站标识,下划线后是数据版本</FONT> </label></td> </tr> <tr> <td width="20%" height="30" align="center" class="borderbottomright">描述</td> <td width="80%" class="borderbottom"><label> <textarea name="descInfo" style="width: 310px;" rows="5"></textarea> </label></td> </tr> <tr> <td height="30" colspan="2" align="center"> <input type="submit" name="save" class="myBtn" value="保 存" onclick="submitForm();" /> <input type="button" name="backtrack" class="myBtn" value="返 回" onclick="goback();"/> </td> </tr> </table> <table width="99%" cellpadding="0" cellspacing="0" align="center" style="margin-top: 20px;"> <tr> <td align="center"><font color="red" style="font-size: 15px;"> <c:if test="${result!=null }"> ${result } </c:if> </font> </td> </tr> </table> </form> </body>
后台代码:
package com.book.action; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import com.book.utils.ftpUtil; @Controller public class Ceshiaction { @RequestMapping("shangc") public String UploadFile(HttpServletRequest request)throws IllegalStateException, IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request; // 获取上传的文件 MultipartFile multiFile = multipartRequest.getFile("dataFile"); //转为流 InputStream inputStream = multiFile.getInputStream(); //获取上传的全名 String originalFilename = multiFile.getOriginalFilename(); //截取后4位 String substring = originalFilename.substring(originalFilename.length()-4,originalFilename.length()); //生成当前时分秒 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String format = simpleDateFormat.format(new Date()); //生成6位随机数 Random random = new Random(); int i = random.nextInt(100000); //拼接新的名称 StringBuffer jp = new StringBuffer(); jp.append(format); jp.append(i); jp.append(substring); System.out.println(jp.toString()); //调用工具类传入相应的值
//FTP地址 端口 帐号 密码 FTP中路径 文件名称 文件流 ftpUtil.uploadFile("192.168.1.91", 21, "txw", "123456", "/", jp.toString(), inputStream); return "ok"; } }
标签:
原文地址:http://www.cnblogs.com/liuJava/p/4959985.html