代码如下:import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileUploadException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;import sun.net.ftp.FtpClient;import com.airchina.cabin.web.form.FtpAttachForm;
public class FtpAttachAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception {
FtpClient ftpClient = null;
FtpAttachForm theForm = (FtpAttachForm) form;
//String fileId = theForm.getFileId();
//String selectedFtpFile = theForm.getSelectedFtpFile();
/** actionform可以记住上一次传来的文件名称,因为,他们是在一个request环境中的 */ //String selectedFtpUploadFile = theForm.getSelectedFtpUploadFile(); FormFile file = theForm.getFile();
String fileName = file.getFileName();
InputStream is = null;
OutputStream os = null;
try {
is = file.getInputStream();
int index = fileName.indexOf(".");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String firstPart = sdf.format(new Date())+fileName.substring(0, index);
String lastPart = fileName.substring(index, fileName.length()); String uploadFileName = firstPart + lastPart;
ftpClient = new FtpClient();
//报错位置,提示我服务器拒绝连接 ftpClient.openServer("196.111.123.11",80); //地址和端口号
/** 打开服务器* */
ftpClient.login("xxx","xxx");
/** 登录到服务器 */
//ftpClient.cd(""); //Configer.getInstance("filePath").get(fileId)
/** 获得要访问的ftp服务器上的目录,文件将存到该目录下去 */
ftpClient.binary();
os = ftpClient.put(uploadFileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1)/** 一次传多少字节 */
{
os.write(buffer, 0, bytesRead);
} return mapping.findForward("success"); } catch (Exception e) {
e.printStackTrace();
throw new FileUploadException("文件上传失败!");
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
if (os != null) {
try {
os.close();
} catch (Exception e) {
}
}
if (file != null) {
file.destroy();
}
}
}
}
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileUploadException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;import sun.net.ftp.FtpClient;import com.airchina.cabin.web.form.FtpAttachForm;
public class FtpAttachAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception {
FtpClient ftpClient = null;
FtpAttachForm theForm = (FtpAttachForm) form;
//String fileId = theForm.getFileId();
//String selectedFtpFile = theForm.getSelectedFtpFile();
/** actionform可以记住上一次传来的文件名称,因为,他们是在一个request环境中的 */ //String selectedFtpUploadFile = theForm.getSelectedFtpUploadFile(); FormFile file = theForm.getFile();
String fileName = file.getFileName();
InputStream is = null;
OutputStream os = null;
try {
is = file.getInputStream();
int index = fileName.indexOf(".");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String firstPart = sdf.format(new Date())+fileName.substring(0, index);
String lastPart = fileName.substring(index, fileName.length()); String uploadFileName = firstPart + lastPart;
ftpClient = new FtpClient();
//报错位置,提示我服务器拒绝连接 ftpClient.openServer("196.111.123.11",80); //地址和端口号
/** 打开服务器* */
ftpClient.login("xxx","xxx");
/** 登录到服务器 */
//ftpClient.cd(""); //Configer.getInstance("filePath").get(fileId)
/** 获得要访问的ftp服务器上的目录,文件将存到该目录下去 */
ftpClient.binary();
os = ftpClient.put(uploadFileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1)/** 一次传多少字节 */
{
os.write(buffer, 0, bytesRead);
} return mapping.findForward("success"); } catch (Exception e) {
e.printStackTrace();
throw new FileUploadException("文件上传失败!");
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
if (os != null) {
try {
os.close();
} catch (Exception e) {
}
}
if (file != null) {
file.destroy();
}
}
}
}
解决方案 »
- struts2的配置问题
- tomcat自动加载(非reloadable参数)求助
- Struts 2中的OGNL无法解析isXXX()?
- servlet 中控制 serial port
- webwork 小问题
- useUnicode=true&characterEncoding=GB2312怎么不起作用?
- tomcat 的问题!!!!!!!!!!!!!!!
- 哪位能给说说如何控制JBoss热部署很多jar时,如何来控制部署的顺序!
- weblogic问题
- Java Project 如何根据build.xml生成war包
- synchronized并非锁住自己的家门,而是绑住自己?
- 详细设计的输入输出指的是什么
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:461)
at java.net.Socket.connect(Socket.java:411)
at sun.net.NetworkClient.doConnect(NetworkClient.java:139)
at sun.net.NetworkClient.openServer(NetworkClient.java:118)
at sun.net.ftp.FtpClient.openServer(FtpClient.java:423)
at com.airchina.cabin.web.action.FtpAttachAction.execute(FtpAttachAction.java:50)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1077)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:465)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:28)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:27)
at com.airchina.cabin.core.common.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:91)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:27)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:7053)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3902)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2773)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)