Struts1.3上传图片
uploadFile.jsp:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for UploadFileForm form</title>
</head>
<body>
<html:form action="/uploadFile">
file : <html:file property="file"/><html:errors property="file"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
public ActionForward uploadfile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UploadActionForm uaf = (UploadActionForm)form;
String fileName = null;
FormFile formFile = uaf.getMyFile();
if (!"".equals(uaf.getMyFileName())){
String fileExt = formFile.getFileName().substring(formFile.getFileName().indexOf("."));
fileName = uaf.getMyFileName()+fileExt;}
else fileName = formFile.getFileName();
FileOutputStream fos = new FileOutputStream("d:\\"+fileName);
fos.write(formFile.getFileData());
}
uploadFile.jsp:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for UploadFileForm form</title>
</head>
<body>
<html:form action="/uploadFile">
file : <html:file property="file"/><html:errors property="file"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
public ActionForward uploadfile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UploadActionForm uaf = (UploadActionForm)form;
String fileName = null;
FormFile formFile = uaf.getMyFile();
if (!"".equals(uaf.getMyFileName())){
String fileExt = formFile.getFileName().substring(formFile.getFileName().indexOf("."));
fileName = uaf.getMyFileName()+fileExt;}
else fileName = formFile.getFileName();
FileOutputStream fos = new FileOutputStream("d:\\"+fileName);
fos.write(formFile.getFileData());
}