SpringMvc 中FTP上传数据
导入包Ftp包 commons-net-3.3.jar
直接例子:
Ftp工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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>
后台代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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()); //调用工具类传入相应的值<br> //FTP地址 端口 帐号 密码 FTP中路径 文件名称 文件流 ftpUtil.uploadFile( "192.168.1.91" , 21 , "txw" , "123456" , "/" , jp.toString(), inputStream); return "ok" ; } } |
· AES 加密模式演进:从 ECB、CBC 到 GCM 的 C# 深度实践
· InnoDB为什么不用跳表,Redis为什么不用B+树?
· 记一次 C# 平台调用中因非托管 union 类型导致的内存访问越界
· [EF Core]聊聊“复合”属性
· 那些被推迟的 C# 14 特性及其背后的故事
· 博客园出海记-开篇:扬帆启航
· 关于布尔类型的变量不要加 is 前缀,被网友们吐槽了,特来完善下
· 30 岁 Java 仍在 “霸榜“:开发者凭什么还在为它熬夜?
· GPT5写5000行代码,行不行?
· 扣子(Coze),开源了!Dify 天塌了