struts2上传图片

来源:互联网 发布:java招聘要求 编辑:程序博客网 时间:2024/05/21 21:36

1.导入jar 包

commons-io-【version】

commons-fileupload-【version】

commons-logging-【version】

freemarker-【version】

log4j-【version】

ognl-【version】

struts2-core-【version】 

xwork-core-【version】

 

2.配置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">

 <filter>

  <filter-name>struts2-cleanUp</filter-name>

  <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>

  </filter>

  <filter-mapping>

  <filter-name>struts2-cleanUp</filter-name>

  <url-pattern>/*</url-pattern>

  </filter-mapping>

  

  <filter>

  <filter-name>struts2</filter-name>

  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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>

 

 

 

3.创建 FileUploadAction

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private String savePath;
private String myFileFileName;//获取的文件名
private File myFile;
private String myFileContentType;//获取文件的类型
private String fileName;
public File getMyFile() {
  return myFile;
}
public void setMyFile(File myFile) {
  this.myFile = myFile;
}

public String getMyFileFileName() {
  return myFileFileName;
}
public String getMyFileContentType() {
  return myFileContentType;
}
public void setMyFileFileName(String myFileFileName) {
  this.myFileFileName = myFileFileName;
}
public void setMyFileContentType(String myFileContentType) {
  this.myFileContentType = myFileContentType;
}

public String getFileName() {
  return fileName;
}
public void setFileName(String fileName) {
  this.fileName = fileName;
}
public void setSavePath(String savePath) {
  this.savePath = savePath;
}
public String getSavePath() {
  return savePath;
}
public String execute() throws Exception {
   //获取文件路径
  String path = ServletActionContext.getServletContext().getRealPath(savePath);
  File imageFile = new File(path + "//" +myFileFileName);
  System.out.println(imageFile);
  BufferedInputStream bis = null;//获取一个上传文件的输入流
  BufferedOutputStream bos = null;//获取一个上传文件的输出流
  try {
   bis = new BufferedInputStream(new FileInputStream(myFile));
   bos = new BufferedOutputStream(new FileOutputStream(imageFile));
  
   byte buf[] = new byte[(int)myFile.length()];
   int length = 0;
   while((length = bis.read(buf)) != -1) {
    bos.write(buf, 0, length);
   }
  
  } catch (Exception e) {
   e.printStackTrace();
  }finally {
   try {
    if(bis != null) {
     bis.close();
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  
   try {
    if(bos != null) {
     bos.close();
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return SUCCESS;
}
}

 

4.配置 struts.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

 <package name="fileUpload" extends="struts-default">

    <action name="fileUpload" class="jxust.zjh.action.FileUploadAction" >

     <param name="savePath">images</param>

     <result name="success">uploadsuc.jsp</result>

    </action>

  </package>

</struts>

 

5,写输入页面

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
   <body>
   <s:form action="fileUpload" method="post" enctype="multipart/form-data">
   <s:file name="myFile" label="选择图片"/>
   <s:submit value="上传"/>
   </s:form>
  </body>

</html>

 


6.显示页面

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>My JSP 'uploadsuc.jsp' starting page</title>
  </head>
  <body>
  <s:debug></s:debug>
    <h2>上传成功</h2>
    <hr>
       <img alt="图片" src="<s:property value="savePath + '/'+myFileFileName" />">
  </body>
</html>

</html>