struts2——上传图片格式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="/struts-tags" prefix="s"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>struts2的一个例子</title> 9 </head> 10 <body> 11 <s:form action="photo.action" method="post" enctype="multipart/form-data"> 12 <s:textarea name="username" label="用户名"/> 13 <s:file name="photo" label="请选择上传图片"/> 14 <s:submit value="提交"/> 15 </s:form> 16 17 </body> 18 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 3 <filter> 4 <filter-name>struts2</filter-name> 5 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 6 </filter> 7 <filter-mapping> 8 <filter-name>struts2</filter-name> 9 <url-pattern>/*</url-pattern> 10 </filter-mapping> 11 <display-name></display-name> 12 <welcome-file-list> 13 <welcome-file>index.jsp</welcome-file> 14 </welcome-file-list> 15 </web-app>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true"/> 7 <package name="hello" extends="struts-default" namespace="/"> 8 <action name="photo" class="com.xiaostudy.web.UpPhoto" method="upPhoto"> 9 <interceptor-ref name="defaultStack"> 10 <param name="fileUpload.allowedExtensions">.jpeg,.jpg,.gif,.png</param> 11 </interceptor-ref> 12 <result name="success">/ok.jsp</result> 13 <result name="input">/err.jsp</result> 14 </action> 15 </package> 16 </struts>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.xiaostudy.web; 2 3 import java.io.File; 4 5 import org.apache.struts2.ServletActionContext; 6 7 import com.opensymphony.xwork2.ActionSupport; 8 9 public class UpPhoto extends ActionSupport { 10 11 public String username; 12 public File photo; 13 public String photoFileName; 14 public String photoContentType; 15 16 public String upPhoto() { 17 18 String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files"); 19 File file2 = new File(path); 20 if(!file2.exists()) { 21 file2.mkdirs(); 22 System.out.println("创建了文件夹》》》》》》"); 23 } 24 File file3 = new File(file2, photoFileName); 25 photo.renameTo(file3); 26 27 System.out.println(photo); 28 System.out.println(file2); 29 System.out.println(file3); 30 31 32 return SUCCESS; 33 } 34 35 public String getUsername() { 36 return username; 37 } 38 39 public void setUsername(String username) { 40 this.username = username; 41 } 42 43 public File getPhoto() { 44 return photo; 45 } 46 47 public void setPhoto(File photo) { 48 this.photo = photo; 49 } 50 51 public String getPhotoFileName() { 52 return photoFileName; 53 } 54 55 public void setPhotoFileName(String photoFileName) { 56 this.photoFileName = photoFileName; 57 } 58 59 public String getPhotoContentType() { 60 return photoContentType; 61 } 62 63 public void setPhotoContentType(String photoContentType) { 64 this.photoContentType = photoContentType; 65 } 66 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>struts2的一个例子</title> 8 </head> 9 <body> 10 okokokok 11 </body> 12 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 不是照片格式 11 </body> 12 </html>