别的不多说直接开整,没有注释和配置文件,不过程序是通的
上传页面 upload.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body>
<form action="upLoad..do method="post" enctype="multipart/form-data"
name="form1">
<p>
上传到服务器并保存到数据库!
<input name="theFile" type="file" id="theFile">
</p>
<p>
<select name="type" value="">
<option>choose</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</p>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
</body>
</html>
转发后的jsp页 saveOk.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
strutsUploadResult
</title>
</head>
<body bgcolor="#ffffff">
<h1>
上传结果
</h1>
文件名:<%=request.getAttribute("fileName")%><br />
模板类型:<%=request.getAttribute("type")%><br />
文件类型:<%=request.getAttribute("fileType")%><br />
文件大小:<%=request.getAttribute("fileSize")%><br />
</body>
</html>
ActionForm UpLoadForm .java
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class UpLoadForm extends ActionForm {
private String type;
private org.apache.struts.upload.FormFile theFile;
public org.apache.struts.upload.FormFile getTheFile() {
return theFile;
}
public void setTheFile(org.apache.struts.upload.FormFile theFile) {
this.theFile = theFile;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
// if (!this.getTheFile().getContentType().equals("image/pjpeg")) {
// }
//if (this.getTheFile().getFileSize() > 100) {
//}
// return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
theFile = null;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Action UpLoadAction.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
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.caexpo.bean.Path;
import com.caexpo.dao.SaveTemplate;
import com.caexpo.model.WcmTemplate;
import com.caexpo.struts.form.UpLoadForm;
public class UpLoadAction extends Action {
/*
* Generated Methods
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UpLoadForm upLoadForm = (UpLoadForm) form;
// TODO Auto-generated method stub
WcmTemplate wt = new WcmTemplate();
SaveTemplate st = new SaveTemplate();
FormFile file = upLoadForm.getTheFile()
Path filePath = Path.getInstance();
String path = filePath.getPath() + "/upload/" + file.getFileName();
try {
InputStream input = file.getInputStream();
OutputStream output = new FileOutputStream(path);
int bytesReader = 0;
byte[] readbuffer = new byte[8192];
while ((bytesReader = input.read(readbuffer, 0, 8192)) != -1) {
output.write(readbuffer, 0, bytesReader);
}
output.close();
// File read = new File(file.getFileName());
FileInputStream fileinputstream = new FileInputStream(path);
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
String con = new String(bytes);
wt.setContent(con);
wt.setTemplatename(file.getFileName());
wt.setType(upLoadForm.getType());
st.SaveTemplate(wt);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
request.setAttribute("type", upLoadForm.getType());
request.setAttribute("fileName", file.getFileName());
request.setAttribute("fileSize", new Integer(file.getFileSize()));
request.setAttribute("fileType", file.getContentType());
return mapping.findForward("success");
}
}
DAO SaveTemplate .java
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.caexpo.hibernate.HibernateSessionFactory;
public class SaveTemplate {
public void SaveTemplate(WcmTemplate wt) {
Session session = HibernateSessionFactory.getSession();
Transaction tr = session.beginTransaction();
try {
session.save(wt);
tr.commit();
} catch (Exception ex) {
tr.rollback();
System.out.println(ex.getMessage());
} finally {
session.close();
}
}
}