<%@ 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. FBV和CBV的差异

    FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述.   CBV CBV(class base ...

  2. iOS 保存异常日志

    // // AppDelegate.m // test // // Created by Chocolate. on 14-4-16. // Copyright (c) 2014年 redasen. ...

  3. Kubernetes之kubectl常用命令

    最近项目有用到Kubernetes作集群配置,所以学习下相关命令,记录下以备下次使用... kubectl help 显示具体的用法 kubectl controls the Kubernetes c ...

  4. Java模拟并发

    =========================one============================= public class Bingfa { public static void m ...

  5. Django设置中文,和时区、静态文件指向

    #========================================================== # 设置时区 注意注释上面的:LANGUAGE_CODE.TIME_ZONE.U ...

  6. 编译型 解释型 C++工作原理

    C++教程_w3cschool https://www.w3cschool.cn/cpp/ C++工作原理: C++语言的程序因为要体现高性能,所以都是编译型的.但其开发环境,为了方便测试,将调试环境 ...

  7. yum -y install epel-release

    EPEL - Fedora Project Wiki https://fedoraproject.org/wiki/EPEL

  8. Java 之 GUI 编程

    GUI (Graphical User Interface, 图形用户接口) CLI (Command line User Interface, 命令行用户接口) Java 为 GUI 提供的对象都存 ...

  9. SQL小练习

    1.现在有两张表订单表TB_ORDER,包括字段:order_id(订单号),username(用户名),amount(订单金额),order_time(下单时间), product_id(商品ID) ...

  10. Linux(1)- 服务器核心知识、Linux入门、VMware与centeos安装、远程连接linux、linux基本命令使用

    一.服务器核心知识 1.电脑和电脑的硬件组成 现在的人们几乎无时无刻不在使用着电脑!不管是桌上型电脑(桌机).笔记型电脑(笔电).平板电脑,还是智慧型手机等等,这些东西都算是电脑.虽然接触这么多,但是 ...