struts2上传图片功能实现(单个图片)

1.搭建struts2框架

   a)导入相关的jar包

           blank实例中的包即可

   b)配置web.xml文件

          

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
   <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
    c)配置struts2.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.enable.DynamicMethodInvocation" value="false" />
    <!-- 开启开发模式,这样可以打印出详细的错误信息 -->
    <constant name="struts.devMode" value="true" />
    
    <!-- 访问后缀 ,,表示省略不写-->
	<constant name="struts.actions.extension" value="action,do,,"/>
	
	<!-- 当struts.xml文件被修改后自动重新部署 -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	
	<!-- 允许上传的文件的最大字节数 -->
	<constant name="struts.multipart.maxSize" value="2097152"/>
	
	<!-- 缓存路径 -->
	<constant name="struts.multipart.saveDir" value="E:\"></constant>
	
    <package name="default" namespace="/" extends="struts-default">
        <action name="*_*" class="com.lsw.struts2_upload.actions.{1}Action" method="{2}">
            <!-- 配置保存路径 -->
            <param name="savePath">/uploadFile</param>
            <result name="success">/WEB-INF/pages/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

2.index.jsp中创建表单(注意红色字体)

      <form action="Upload_upload" method="post" enctype="multipart/form-data">
            文件:<input type="file" name="file">
            <input type="submit" value="上传">
    </form>

3.action类(UploadAction)package com.lsw.struts2_upload.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{
    private File file;//要上传的表单元素的name属性值
    private String fileContentType;//上传文件的类型
    private String fileFileName;//上传文件的名称
    private String savePath;//用来接受依赖注入的文件保存路径
    
    public String upload(){
        FileOutputStream fos=null;
        FileInputStream  fis=null;
        
        
        try {
            fos=new FileOutputStream(getSavePath()+"\\"+getFileFileName());
            fis=new FileInputStream(file);
            byte[] buffer=new byte[1024];
            int len=0;
            while((len=fis.read(buffer))>0){
                fos.write(buffer,0,len);
            }
            fos.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "success";
        
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
}



ServletActionContext.getServletContext().getRealPath(savePath)
:web容器中对于路径都是获取发布项目当前Root下的路径 也就是/跟路径 而非磁盘上任意路径。
ServletActionContext.getServletContext().getRealPath("/files");
获取的是你项目所在的路径。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值