日期:2014-05-19 浏览次数:20750 次
<form name="addform" action="candiate/candiateadd.action" method="post" enctype ="multipart/form-data"> <input id="zhuceImage" name="upfile"type="file"/> <input id="zhuceBimage" name="upfile" type="file"/> <input id="zhuceName" name="candiate.candiateName" type="text"/> </form
public class CandiateAction { private Candiate candiate; private List<File> upfile; private List<String> fileFileName; private List<String> fileContentType; get set方法 public String candiateadd() throws IOException{ //得到工程保存图片的路径 String address = ServletActionContext.getServletContext().getRealPath("/images/upload"); String relateAddress = "images/upload";//保存到数据库的相对路径 System.out.println(address); System.out.println(upfile.size()); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String dateTime = sdf.format(new Date()); //循环上传的文件 for(int i = 0 ; i < upfile.size() ; i ++){ InputStream is = new FileInputStream(upfile.get(i)); //得到图片保存的位置(根据address来得到图片保存的路径在tomcat下的该工程里) [color=#FF0000]File destFile = new File(address,this.getFileFileName().get(i)+dateTime);[/color] [color=#FF0000]this.getFileFileName().get(i)取不到上传文件的名字[/color] //把图片写入到上面设置的路径里 OutputStream os = new FileOutputStream(destFile); byte[] buffer = new byte[400]; int length = 0 ; while((length = is.read(buffer))>0){ os.write(buffer, 0, length); } is.close(); os.close(); } //将上传的文件路径存入数据库 candiate.setCandiateImage(relateAddress+"/"+this.getFileFileName().get(0)+dateTime); candiate.setCandiateBimage(relateAddress+"/"+this.getFileFileName().get(1)+dateTime); return "index"; } }
<package name="candiateadd" namespace="/candiate" extends="struts-default"> <action name="candiateadd" class="com.tyut.actions.CandiateAction" method="candiateadd"> <interceptor-ref name="candiateadd"> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png </param> </interceptor-ref> <interceptor-ref name="defaultStack"/> <result name="success">/index.jsp</result> </action> </package>
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list>