<%@ 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. Windows按名称排序问题

    偶然发现一个按名称排序的文件夹内,文件顺序是混乱的,例如: 在一个文件夹内建立如下三个文件: 0F.txt 1A.txt 02.txt 按名称/升序排列, 将得到上述结果,0F在最前,02在最后. 百 ...

  2. mysql死锁-非主键索引更新引起的死锁

    背景:最近线上经常抛出mysql的一个Deadlock,细细查来,长了知识! 分析:错误日志如下: 21:02:02.563 ERROR dao.CommonDao        [pool-15-t ...

  3. Scala学习之For、Function、Lazy(4)

    1.for的使用 for的使用在各种编程语言中是最常见的,这里只是聊聊for在Scala中的表现形式,由于Scala语言是完全面向对象的,所以直接导致for的不同呈现,下面举几个例子说明一下 obje ...

  4. Too Many Open Files的错误

    百度Elasticsearch-产品描述-介绍-百度云 https://cloud.baidu.com/doc/BES/FAQ.html#Too.20Many.20Open.20Files.E7.9A ...

  5. HTTP缓存实现的原理

    浏览器是如何知道使用缓存的,其实这都是通过http中,浏览器将最后修改时间发送请求给web服务器,web服务器收到请求后跟服务器上的文档最后修改的时间对比,如果web服务器上最新文档修改时间小于或者等 ...

  6. BaseDao 接口

    // 以后所有的 Dao 接口都需要继承 BaseDao 接口; // 自定义泛型接口 public interface BaseDao<T>{ public void save(T t) ...

  7. FatMouse and Cheese---hdu1078(记忆化搜索=搜索+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意就是有n*n的地图,每个地方都有食物,数量不同,老鼠在(0,0)的位置每次它最多跳 k 步, ...

  8. 关于Nginx部署Django项目的资料收集

    参考:https://www.cnblogs.com/chenice/p/6921727.html 参考:https://blog.csdn.net/fengzq15/article/details/ ...

  9. 我的Android进阶之旅------&gt;Android无第三方Jar包的源代报错:The current class path entry belongs to container ...的解决方法

    今天使用第三方Jar包afinal.jar时候.想看一下源码,无法看 然后像加入jar相应的源代码包.也无法加入相应的源代码,报错例如以下:The current class path entry b ...

  10. 对数值数据的格式化处理(保留小数点后N位)

    项目中有时会遇到对数值部分进行保留操作,列如保留小数点后2位,所有的数据都按这种格式处理, //保留小数点后2位,都按这种格式处理,没有补0 DecimalFormat df = new Decima ...