首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2.1.6实现(图片)文件的下传

2012-10-09 
struts2.1.6实现(图片)文件的上传1.首先创建一个FileUploadAction package jxust.zjh.action import java

struts2.1.6实现(图片)文件的上传
1.首先创建一个FileUploadAction
package jxust.zjh.action;
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;
}
}
2.配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <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>
</web-app>
3.配置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" >
  <param name="savePath">images</param>
  <result name="success">/uploadSuc.jsp</result>
  </action>
  
  </package>
</struts>
4.建一个上传页面index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ 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>
5.上传成功后转向的页面uploadsuc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  
  </head>
  
  <body>
  <s:debug></s:debug>
    <h2>上传成功</h2>
    <hr>
<img alt="struts2.1.6实现(图片)文件的下传" src="<s:property value="savePath + '/'+myFileFileName" />">
  </body>
</html>
运行时服务器控制台有如下输出 Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
原因是struts.multipart.saveDir没有配置。struts.multipart.saveDir用于指定存放临时文件的文件夹,该配置写在struts.properties文件中。
可以在struts.properties文件加入如下代码解决:
struts.multipart.saveDir = /images
:上传成功的图片并不存放在myeclipse的workspace项目中,而是在tomcat中webapps下对应的项目中

热点排行