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>&nbsp;<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&nbsp;&nbsp;&nbsp;&nbsp;下划线前是基站标识,下划线后是数据版本</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();" />  
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
          <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"
}

  

posted @   Lars  阅读(1944)  评论(0)    收藏  举报
(评论功能已被禁用)
编辑推荐:
· 一个 java 空指针异常的解决过程
· 揭开 SQL Server 和 PostgreSQL 填充因子的神秘面纱
· 没有调度器的协程不是好协程,零基础深入浅出 C++20 协程
· 别做抢活的导演:代码中的抽象层次原则
· 从 Redis 客户端超时到 .NET 线程池挑战
阅读排行:
· 会Vibe Coding的同事:我一个人干掉整个技术部!
· 回答准确率从60%飙至95%!AI知识库救命方案
· 揭开SQL Server和PostgreSQL填充因子的神秘面纱
· dotnetty 内存泄漏的BUG修复了
· 20250709 - GMX V1 攻击事件: 重入漏洞导致的总体仓位价值操纵
点击右上角即可分享
微信分享提示