struts部分:
attribute="upfileForm"
input="/upload/uploadfile.jsp"
name="upfileForm"
path="/upfile"
scope="request"
validate="true"
type="com.yourcompany.struts.action.UpfileAction">
public class UpfileAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm upfileForm = (DynaActionForm) form;// TODO Auto-generated method stub
Image image = new Image();
String strimgname = upfileForm.getString("filename");
image.setImgname(strimgname);
imageDAO.save(image);
FormFile filedata = (FormFile)upfileForm.get("filedata");
ServletContext servletContext = this.getServlet().getServletContext();
String filePath = servletContext.getRealPath("/");
try {
InputStream stream = filedata.getInputStream();
OutputStream bos = new FileOutputStream(filePath + "/image/" + filedata.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 (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward("ok");
}
private ImageDAO imageDAO;
public void setImageDAO(ImageDAO dao){
this.imageDAO = dao;
}
}
public class DisplayimgAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
List results = imageDAO.findAll();
if(results!=null){
HttpSession session=request.getSession();
session.setAttribute("results",results);
return mapping.findForward("ok");
}return mapping.findForward("fail");
}
private ImageDAO imageDAO;
public void setImageDAO(ImageDAO dao){
this.imageDAO = dao;
}
}
ImageDAO.java
public void save(Image transientInstance) {
Session session =this.getSession();
Transaction tx = null;
tx = session.beginTransaction();
session.save(transientInstance);
tx.commit();
session.evict(transientInstance);
}
public List findAll() {
Session session =this.getSession();
Query query = session.createQuery("from Image");
return query.list();
}
displayimg.jsp
uploadfile.jsp
filename :
filedata :
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
*.do
encodingFilter
*.jsp