struts2——上传图片格式

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>struts2的一个例子</title>
</head>
<body>
<s:form action="photo.action" method="post" enctype="multipart/form-data">
<s:textarea name="username" label="用户名"/>
<s:file name="photo" label="请选择上传图片"/>
<s:submit value="提交"/>
</s:form> </body>
</html>

index.jsp代码

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<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>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

web.xml代码

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="hello" extends="struts-default" namespace="/">
<action name="photo" class="com.xiaostudy.web.UpPhoto" method="upPhoto">
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">.jpeg,.jpg,.gif,.png</param>
</interceptor-ref>
<result name="success">/ok.jsp</result>
<result name="input">/err.jsp</result>
</action>
</package>
</struts>

struts.xml代码

 package com.xiaostudy.web;

 import java.io.File;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionSupport;

 public class UpPhoto  extends ActionSupport {

     public String username;
public File photo;
public String photoFileName;
public String photoContentType; public String upPhoto() { String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files");
File file2 = new File(path);
if(!file2.exists()) {
file2.mkdirs();
System.out.println("创建了文件夹》》》》》》");
}
File file3 = new File(file2, photoFileName);
photo.renameTo(file3); System.out.println(photo);
System.out.println(file2);
System.out.println(file3); return SUCCESS;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public File getPhoto() {
return photo;
} public void setPhoto(File photo) {
this.photo = photo;
} public String getPhotoFileName() {
return photoFileName;
} public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
} public String getPhotoContentType() {
return photoContentType;
} public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
}

action动作类UpPhoto

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>struts2的一个例子</title>
</head>
<body>
okokokok
</body>
</html>

ok.jsp代码

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
不是照片格式
</body>
</html>

err.jsp代码

上一篇:HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)


下一篇:springBoot之配置文件的读取以及过滤器和拦截器的使用