程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> struts構建文件上傳(六)

struts構建文件上傳(六)

編輯:關於JAVA

這是action頁面,

package tester.business.maitain;
import tclcc.tester.util.Selector;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;
import org.apache.struts.upload.FormFile;
import java.io.*;
public class MaintainAction
extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
/**@todo: complete the business logic here, this is just a skeleton.*/
MaintainForm maintainForm = (MaintainForm) actionForm;
TrainPlanDAO trainPDAO = new TrainPlanDAO();
Trainplan trainPlan = new Trainplan();
trainPlan = maintainForm.getTrainPlan();
FormFile theFile = null;
theFile = maintainForm.getTheFile1();
String p_accessory;
p_accessory = theFile.getFileName();
try {
InputStream stream = theFile.getInputStream(); //把文件讀入
String filePath = httpServletRequest.getRealPath("/"); //取當前系統路徑
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "\\sub" + "/" +
theFile.getFileName()); //建立一個上傳文件的輸出流
//System.out.println(filePath+"/"+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead); //將文件寫入服務器
}
bos.close();
stream.close();
} catch (Exception e) {
System.err.print(e);
}
try {
trainPlan.setP_accessory(p_accessory);
trainPDAO.addTrainPlan(trainPlan);//調用數據庫插入方法
System.out.println("success");
} catch (Exception ex) {
}
httpServletRequest.setAttribute("trainPlain", trainPlan);
return (actionMapping.findForward("trainplanCreated"));
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved