文件上传代码

发表于: java/j2ee | 作者: | 日期: 2008/11/05 06:11
标签:

package com.sitech.grp.web.actionbean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import java.io.*;
import com.sitech.grp.model.Dgrpcustcontract;
import com.sitech.grp.web.formbean.DGrpContractForm;
import com.sitech.grp.service.IDGrpContractManager;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class DGrpContractAction extends DispatchAction {
private IDGrpContractManager grpContractManager;
private FtpClient ftpClient;
// 查询集团合同信息
public ActionForward queryGrpContractMsg(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
DGrpContractForm frm = (DGrpContractForm) form;
try {
Dgrpcustcontract dGrpContract = frm.getEntity();
frm.setResultList(grpContractManager
.queryGrpContractMsg(dGrpContract.getUnit_id()));
} catch (Exception e) {
System.out.println(e);
}
return mapping.findForward(“querygrpcontract”);
}

// 查询集团合同详细信息
public ActionForward querySingleContractMsg(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
DGrpContractForm frm = (DGrpContractForm) form;
try {
Dgrpcustcontract dGrpContract = frm.getEntity();
dGrpContract = grpContractManager.get(dGrpContract.getUnit_id());
frm.setEntity(dGrpContract);
} catch (Exception e) {
System.out.println(“querySingleContractMsg=” + e);
}
return mapping.findForward(“querySingleContractMsg”);
}

// 更新集团合同信息
public ActionForward updateContractMsg(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception{
DGrpContractForm frm = (DGrpContractForm) form;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
Dgrpcustcontract dGrpContract = frm.getEntity();
// ——————————————–
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase(“utf-8”))) {
response.setContentType(“text/html; charset=gb2312″);// 如果没有指定编码,编码格式为gb2312
}
FormFile file = frm.getFiles();// 取得上传的文件
if (file.getFileName().length() > 0) {
InputStream stream = file.getInputStream();// 把文件读入
// server:FTP服务器的IP地址;user:登录FTP服务器的用户名
// password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
ftpClient = new FtpClient();
String path=”/bboss/run/vippage/upload/contract/”;//上传文件路径
ftpClient.openServer(“10.161.1.58”);
ftpClient.login(“weblogic”, “asd123”);
//path是ftp服务下主目录的子目录
if (path.length() != 0)
ftpClient.cd(path);
//用2进制上传
ftpClient.binary();
//”upftpfile”用ftp上传后的新文件名
os = ftpClient.put(dGrpContract.getContract_id() + “-”
+ file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);// 将文件写入服务器}
}

dGrpContract.setContract_file(file.getFileName());
}
// ——————————————–
grpContractManager.update(dGrpContract);
frm.setResultList(grpContractManager
.queryGrpContractMsg(dGrpContract.getUnit_id()));
frm.setOp_flag(“true”);
} catch (Exception e) {
frm.setOp_flag(“false”);
System.out.println(“updateContractMsg=” + e);
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return mapping.findForward(“querygrpcontract”);
}

public void setGrpContractManager(IDGrpContractManager grpContractManager) {
this.grpContractManager = grpContractManager;
}

}

: https://blog.darkmi.com/2008/11/05/315.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.