<%@ 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代码

struts2——上传图片格式的更多相关文章

  1. struts2上传图片的全过程

    struts2上传图片的过程 1.写一个上传的jsp页面upload_image.jsp,内容如下:<body><center>    <font color=" ...

  2. struts2上传图片超过大小给出错误提示

    struts2上传图片超过大小给出错误提示        今天碰到上传图片太大,上传不上去返回input视图的界面,回显的错误信息却是乱码,整了好久才整出来,在这里做个记录,方便自己以后查阅,也希望能 ...

  3. JS判断上传图片格式是否正确

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. Struts2 输入格式自动校验的一些注意事项

    Struts2 在配置格式校验的文件的时候,格式是XXAction-validation.xml,具体如下. 需要注意的是: field的name属性的值,必须要和jsp中表单提交的name一致.千万 ...

  5. struts2上传图片

    在WEB-INF下新建一个content目录,建立一个upload.jsp <%@ page contentType="text/html; charset=UTF-8" l ...

  6. 关于struts2上传图片临时文件

  7. Struts2上传图片时报404错误

    可能是struts配置文件中定义的拦截器导致的,后缀拦截导致,将该拦截器去掉,在action类里判断后缀 public String upload()throws Exception{ ActionC ...

  8. 一个简单的struts2上传图片的例子

    https://www.cnblogs.com/yeqrblog/p/4398914.html 在我的大创项目中有对应的应用

  9. Web Api 上传图片,解决上传图片无格式

    制作这个功能时,找了很多资料,不过忘记了地址,所以就不一一放连接了, 直接上代码吧! 1. 首先新建一个上传的控制器 /// <summary> /// 上传 /// </summa ...

随机推荐

  1. XMLHttpRequest 对象 status 和statusText 属性对照表

    XMLHttpRequest 对象 status 和statusText 属性对照表 status statusText 说明 0** - 未被始化 1** - 请求收到,继续处理 100 Conti ...

  2. mysql5.7 安装版安装

    参考 http://dev.mysql.com/doc/refman/5.7/en/installing.html 下载mysq5.7的安装包 http://dev.mysql.com/downloa ...

  3. less-!important关键字

    //!important关键字 使用!important关键字混入调用之后,以标记它继承的所有属性!important,example: .test{ background:red; font-siz ...

  4. 几种常见数据库查询判断表和字段是否存在sql

    1.MSSQL Server    表:select COUNT(*) from dbo.sysobjectsWHEREname= 'table_name':  字段:select COUNT(*)  ...

  5. When an HTTP server receives a request for a CGI script

    cgicc: Overview of the Common Gateway Interface https://www.gnu.org/software/cgicc/doc/cgi_overview. ...

  6. Windows数据库定时备份

    首先打开:任务计划程序 右键任务计划程序库,选择创建基本任务 然后即可以按照实际情况逐步进行 直到启动程序--浏览(程序或脚本)时,这里本人导入的是backup.bat文件,文件内容为 @echo 设 ...

  7. Shell正则表达式和文本处理工具

    作业一:整理正则表达式博客 一.什么是正则 正则就是用一些具有特殊含义的符号组合而成(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 通配符是由shell解释得. ...

  8. 手动拼写出来的sp_who结果集

    SELECT SPID = er.session_id  ,STATUS = ses.STATUS  ,[Login] = ses.login_name  ,Host = ses.host_name  ...

  9. phpcms 列表页中,如何调用其下的所有子栏目?

    {pc:content action="category" catid="$catid" num="99" order="list ...

  10. appium入门基础

    1. 建立session时常用命令: DesiredCapabilities cap = new DesiredCapabilities(); cap.SetCapability("brow ...