上传代码:
UploadActionForm.java中内容
package com.seavision.OurSecond.action.knowledge;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
/**
* MyEclipse Struts Creation date: 11-08-2006
*
* XDoclet definition:
*
* @struts.form name="UploadActionForm"
*/
public class UploadActionForm extends ActionForm {
private FormFile file;
private String cataID;
private String cataName;
private String parentID;
private String parentName;
private String fileName;
private String digist;
public String getCataID() {
return cataID;
}
public void setCataID(String cataID) {
this.cataID = cataID;
}
public String getCataName() {
return cataName;
}
public void setCataName(String cataName) {
this.cataName = cataName;
}
public String getDigist() {
return digist;
}
public void setDigist(String digist) {
this.digist = digist;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getParentID() {
return parentID;
}
public void setParentID(String parentID) {
this.parentID = parentID;
}
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
UploadAction.java中内容
package com.seavision.OurSecond.action.knowledge;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
import org.apache.struts.upload.FormFile;
import com.seavision.OurSecond.beans.KnowDocInfoVO;
import com.seavision.OurSecond.beans.UserVO;
import com.seavision.OurSecond.common.BaseAction;
import com.seavision.OurSecond.components.knowledge.KnowLedgeManageFactory;
import com.seavision.OurSecond.components.knowledge.UploadAndDownManage;
/**
* MyEclipse Struts Creation date: 11-08-2006
*
* XDoclet definition:
*
* @struts.action path="/page/knowledge/UploadAction" name="UploadActionForm"
* input="/page/knowledge/ReferKnowledgePage.jsp" scope="request"
*/
public class UploadAction extends BaseAction {
// --------------------------------------------------------- Instance
// Variables
// --------------------------------------------------------- Methods
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadActionForm UploadActionForm = (UploadActionForm) form;
this.initializeAction(request, "PeopleManagePage2Action");
String message=null;
UserVO uservo = (UserVO) this.getAttribute("loginUserBean");
if (this.check(uservo)) {
message = "您还没有登录或您离开系统时间太久,请重新登录";
return this.moveToFailure(message, "logon", mapping);
}
FormFile file=UploadActionForm.getFile();
System.out.println("1.......................");
//获得上传文件的后缀名
String Suffix = file.getFileName().substring(
file.getFileName().lastIndexOf("."));
System.out.println("2.......................");
//获取数据
String cataID=UploadActionForm.getCataID();
String cataName=UploadActionForm.getCataName();
String parentID=UploadActionForm.getParentID();
String parentName=UploadActionForm.getParentName();
String fileName=UploadActionForm.getFileName();
String digist=UploadActionForm.getDigist();
// 由于乱码原因,测试固定值
// if (parentName==null||parentName.equals("")){
// parentName="阳平";
// }
// if (parentID==null||parentID.equals("")){
// parentID="111";
// }
// parentName="parent";
// cataName="cata";
//
// System.out.println("1!!!!!!!!!!!!!!!!!!!1"+ parentName +"!!!!!"+cataName);
// System.out.println(cataID+" ; "+cataName+" ; "+fileName);
//检查文件是否是合法文件
if (!Suffix.equals(".txt")&&!Suffix.equals(".pdf")&&!Suffix.equals(".doc")){
System.out.println("不是合法文件.............");
return mapping.getInputForward();
}
System.out.println("Suffix : "+Suffix);
//设置文件大小
int fileSize=file.getFileSize();
int fileMaxSize=Integer.parseInt("1024100000");
if(fileSize>fileMaxSize){
System.out.println("文件太大........");
return mapping.getInputForward();
}
//创建上传文件所在根目录和子目录
String dir="d:/"+parentName+"/";
String filepath =new String(dir);
File pathName=new File(filepath);
boolean b=pathName.mkdir();
String dir1="d:/"+parentName+"/"+cataName+"/";
String filepath1 =new String(dir1);
File pathName1=new File(filepath1);
boolean b1=pathName1.mkdir();
//上传文件
Date date=new Date();
final String random=String.valueOf(date.getTime());
System.out.println(";;;;;;;;;;;;;;;;;"+date.getTime()+date.getTime());
File uploadfile=new File(dir1+random+fileName+Suffix);
try {
FileOutputStream fout = new FileOutputStream(uploadfile);
fout.write(file.getFileData());
fout.close();
System.out.println("上传文件成功..............");
//调用方法...............cataID,fileName,fileSize,
//digist,uploadDate,authorID,author,dotNum
KnowLedgeManageFactory factory=KnowLedgeManageFactory.getKnowLedgeManageFactory();
UploadAndDownManage uploadAndDownManage=factory.getUploadAndDownManage();
KnowDocInfoVO knowDocInfo=new KnowDocInfoVO();
knowDocInfo.setCataID(Integer.parseInt(cataID));
knowDocInfo.setFileName(random+fileName+Suffix);
knowDocInfo.setFileSize(file.getFileSize());
knowDocInfo.setDigist(digist);
//得到当前日期
Date dates = new Date(System.currentTimeMillis());
DateFormat sdf=SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);
String datetime=sdf.format(date);
Timestamp time=Timestamp.valueOf(datetime);
System.out.println("..............time : "+time);
knowDocInfo.setUploadDate(time);
//得到上传的用户ID和姓名
knowDocInfo.setAuthorID(uservo.getUserID());
knowDocInfo.setAuthor(uservo.getName());
String st=uploadAndDownManage.addKnowDocInfo(knowDocInfo);
if (st.equals("success")) {
return this.moveToSuccess("success", mapping);
} else {
message = " 上传失败!";
return this.moveToFailure(message, "false", mapping);
}
} catch (FileNotFoundException e) {
e.getMessage();
} catch (IOException e) {
e.getMessage();
}
return null;
}
}
下载部分: DownloadAction.java中
package com.seavision.OurSecond.action.knowledge;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.seavision.OurSecond.beans.KnowDocInfoVO;
import com.seavision.OurSecond.beans.UserVO;
import com.seavision.OurSecond.common.BaseAction;
import com.seavision.OurSecond.components.knowledge.CatalogManage;
import com.seavision.OurSecond.components.knowledge.KnowDocManage;
import com.seavision.OurSecond.components.knowledge.KnowLedgeManageFactory;
public class DownloadAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
this.initializeAction(request, "DownloadAction");
// 从session获得loginUserBean
String message = "";
UserVO uservo = (UserVO) this.getAttribute("loginUserBean");
if (this.check(uservo)) {
message = "您还没有登录或您离开系统时间太久,请重新登录";
return this.moveToFailure(message, "logon", mapping);
}
System.out.println("进入DownloadAction....................................");
//获得所有需要数据开始
String docID=this.getParameter("docID");
KnowLedgeManageFactory factory=KnowLedgeManageFactory.getKnowLedgeManageFactory();
KnowDocManage knowDocManage = factory.getKnowDocManage();
CatalogManage catalogManage=factory.getCatalogManage();
KnowDocInfoVO knowDocInfo=knowDocManage.getKnowDoc(docID);
String[] s=catalogManage.getCataAndParent(String.valueOf(knowDocInfo.getCataID()));
String cataName=s[0];
String parentName=s[1];
String dir="d:/"+parentName+"/"+cataName+"/"+knowDocInfo.getFileName();
System.out.println("dir : "+ dir);
File file=new File(dir);
FileInputStream fis=new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buffer=new byte[1024*1024];
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + knowDocInfo.getFileName());
OutputStream os=response.getOutputStream();
for(int len = bis.read(buffer); len != -1; len = bis.read(buffer)){
os.write(buffer, 0, len);
}
bis.close();
fis.close();
os.close();
//下载成功。进行点击数统计
KnowDocManage knowDocManages=factory.getKnowDocManage();
String ret =knowDocManages.updKnowDocDotNum(docID);
if (ret.equals("success")){
return this.moveToSuccess("success", mapping);
}
return this.moveToFailure(message, "false", mapping);
//获得所有需要数据结束
// if (st.equals("success")) {
//
// return this.moveToSuccess("success", mapping);
// } else {
// message = " 编辑失敗!";
// return this.moveToFailure(message, "false", mapping);
// }
} catch (Exception e) {
return this.moveToError(e.getMessage(), "eroor", mapping);
}
}
}