struts1 ftp 文件上传

strtus-config.xml

  <action path="/uploadFtp" type="com.cattsoft.ebook.btsm.util.FileUploadAction"
   validate="false">
   <forward name="view" path="fileUpload/view.jsp" />
  </action>

 

 

 

上传文件的到ftp页面

 

<%@ page contentType="text/html; charset=GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'upload.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form method="post" action="<%=request.getContextPath()%>/btsm/uploadFtp.do">

<input type="file" name="file_name" />

<input type="submit" />

</form>
</body>
</html>

 

 

 

显示上传结果的jsp

 

 

<%@ page contentType="text/html; charset=GBK"%>
<html>
<head>
<style type="text/css">
th
{
background-color: #4455aa;
color: white;
font-size: 14px;
font-weight:bold;
}
td.TableBody1
{
background-color: #FFFFF0;
color: white;
font-size: 14px;
font-weight:bold;
font-color: red;
}
.tableBorder1
{
width:97%;
border: 1px;
background-color: #6595D6;
}
</style>
</head>
<body>
<%String inf = (String) request.getAttribute("inf");
if (inf == null) {
inf = request.getParameter("inf");
}%>
<table class="tableborder1" style="width: 75%;" align="center"
cellpadding="3" cellspacing="1">
<tbody>
<tr align="center">
<th colspan="2" height="25" width="100%">信 息 提 示:</th>
</tr>
<tr align="center">
<%--<td class="tablebody1" colspan="2" width="100%" height="200"><%=inf%></td>--%>
</tr>
<tr align="center">
<td><input name="back" value="返 回" οnclick="history.back();"
type="button" /></td>
</tr>
</tbody>
</table>
</body></html>
 

 

 

 

 

对应的actioin

 


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class FileUploadAction extends Action {


    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws IOException {

        request.setCharacterEncoding("GBK");

        try {
            // 连接ftp服务器
            String fileName = request.getParameter("file_name");
            String result = UpFileUtils.connectServer("192.168.212.123", "imp", "918273imp!@#", "yjh",fileName);
            System.out.println(result);
        } catch (Exception e) {
            System.out.println(e.toString());
            request.setAttribute("inf", e.toString());
           
        }

        return mapping.findForward("view");
    }

   
}

 

 

 

 

action中调用的UpFileUtils

 

 

 


import java.io.FileInputStream;

import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class UpFileUtils {
    public static String connectServer(String server, String user, String password, String path,
            String fileName) throws Exception {
        FtpClient ftpClient = new FtpClient();
        String result = "";
        try {

            // server:FTP服务器的IP地址;user:登录FTP服务器的用户名
            // password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
            ftpClient.openServer(server);
            ftpClient.login(user, password);
            // path是ftp服务下主目录的子目录
            if (path.length() != 0)
                ftpClient.cd(path);
            // 用2进制上传
            ftpClient.binary();
            result = upload(ftpClient, fileName);
        } catch (Exception e) {
            result="false";
            throw e;
        } finally {
            if (ftpClient != null) {
                try{
                    ftpClient.closeServer();
                }catch(Exception e){
                    result="false";
                    throw e;
                }
            }
        }
        return result;

    }

    private static String upload(FtpClient ftpClient, String filename) throws Exception {
        TelnetOutputStream os = null;
        FileInputStream is = null;
        String fileName = filename.substring(filename.lastIndexOf("//") + 1);
        try {
            // "upftpfile"用ftp上传后的新文件名

            os = ftpClient.put(fileName);
            java.io.File file_in = new java.io.File(filename);
            if (file_in.length() == 0) {
                return "the file is null";
            }
            is = new FileInputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
        } finally {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
        }
        return "ok";
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值