当前位置: 代码迷 >> Web前端 >> Struts第八天-文件下传功能
  详细解决方案

Struts第八天-文件下传功能

热度:205   发布时间:2012-10-19 16:53:35.0
Struts第八天----文件上传功能

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <constant name="struts.i18n.encoding" value="utf-8"/>
 <constant name="struts.action.extension" value="do,action"/>
 <constant name="struts.multipart.maxSize" value="10701096"/>
 <package name="employee" namespace="/control/employee" extends="struts-default">
  <action name="list_*" class="com.isoftstone.study.HelloWorldAction" method="{1}">   
   <result name="message">/WEB-INF/page/message.jsp</result> 
  </action>  
 </package> 
</struts>  

 

package com.isoftstone.study;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class HelloWorldAction { 
 private File fileup;
// private String fileupContentType;//得到文件类型
 private String fileupFileName;//上传文件的字段名+相应的信息 后面是固定的
 public String getFileupFileName() {
  return fileupFileName;
 }
 public void setFileupFileName(String fileupFileName) {
  this.fileupFileName = fileupFileName;
 }
 public File getFileup() {
  return fileup;
 }
 public void setFileup(File fileup) {
  this.fileup = fileup;
 }
 public String execute() throws Exception{ 
  String realpath=ServletActionContext.getServletContext().getRealPath("/files");//得到了一个路径
  System.out.println(realpath);
  if(fileup!=null){
   File savefile=new File(new File(realpath),fileupFileName);//先建立文件对象,然后判断存在与否
   if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();   
   FileUtils.copyFile(fileup,savefile);
   ActionContext.getContext().put("message", "上传成功");
  }
  return "message";
 }
 public String ras() throws Exception{  
  return "message";
 }
}

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'fileup.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    <form action="${pageContext.request.contextPath}/control/employee/list_execute.action" enctype="multipart/form-data" method="post">
     文件:<input type="file" name="fileup"/>
     <input type="submit" value="上传文件"/>
    </form>
  </body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'message.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    ${message}<br/>    
  </body>
</html>

 

 

  相关解决方案