struts1.2 上傳檔案

yangjie1981發表於2010-05-10
html:
<form action="uploadrequire.do" method="post" enctype="multipart/form-data" onsubmit="return validataInput()">
<input type="file" id="fileName" name="myfile" />
</form>

struts form:

private FormFile myfile;


public FormFile getMyfile() {
return myfile;
}

public void setMyfile(FormFile myfile) {
this.myfile = myfile;
}

struts action:


public ActionForward uploadcase(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

boolean flag = true;
try {
int number = 0;
String casenumber="0";
ConnectOracleTools contool = new ConnectOracleTools();
Connection con = contool.getConnectionOracle("system", "manager");
String sql = "select * from caseinformation where caseid in (select max(caseid) from caseinformation)";
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery(sql);
while(result.next())
{
casenumber = result.getString("casenumber");
casenumber = casenumber.substring(4);
}
number = Integer.parseInt(casenumber)+1;
CaseInfoForm caseform = (CaseInfoForm) form;
String caseinformation = request.getParameter("caseinformation");
FormFile file = caseform.getMyfile();
caseform.setFileName("case"+number);
int index = file.getFileName().lastIndexOf(".");
String path = this.getServlet().getServletContext().getRealPath("/caseinfo");
File newfile = new File(path+"//"+caseform.getFileName());
newfile.mkdir();
String saveName = newfile.getPath() + File.separator + caseform.getFileName()+file.getFileName().substring(index);


FileOutputStream fileoutput = new FileOutputStream(saveName);
fileoutput.write(file.getFileData());


String caselink = "caseinfo"+"/"+caseform.getFileName()+"/"+caseform.getFileName()+file.getFileName().substring(index);
String casetype = request.getParameter("casetype");
String userid = request.getParameter("userid");
String keywords = request.getParameter("keywords");
String insertsql="insert into caseinformation (caseid,casenumber,caseinfo,caselink,createdate,casemanagerid,casetype,keywords)values (caseinformation_squ.nextval,'"
+caseform.getFileName()+"','"+caseinformation+"','"+caselink+"',sysdate,'"
+userid+"','"+casetype+"','"+keywords+"')";
stmt.executeUpdate(insertsql);
System.out.println("insertsql====="+insertsql);
con.commit();
con.close();
fileoutput.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return mapping.findForward("fail");
}
return mapping.findForward("ok");

}

相關文章