Struts2 实现文件简单上传

Action类代码:

package com.struts2_upload.action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {
 
 private File myFile;//上传的文件
 private String myFileContentType;//上传文件的类型(变量取名必须myFile+ContentType格式)
 private String myFileFileName;//上传文件的名(变量取名必须myFile+FileName格式)
 
 public String execute(){
  
  try {
   //基于image创建一个文件输入流
   InputStream inp=new FileInputStream(myFile);
   
   //设置上传文件目录 
   String path=ServletActionContext.getServletContext().getRealPath("/upload");
   
   //设置目标文件
   File toFile=new File(path,this.getMyFileFileName());
   
   //创建一个输出流
   OutputStream out=new FileOutputStream(toFile);
   
   //设置缓存
   byte [] buffer=new byte[1024];
   int length=0;
   
   //读取image文件输出到toFile文件中
   while ((length=inp.read(buffer))>0) {
    out.write(buffer,0,length);
   }
   System.out.println(this.getMyFileFileName());
   System.out.println(this.getMyFileContentType());
   inp.close(); out.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return SUCCESS;
 }

 public File getMyFile() {
  return myFile;
 }
 public void setMyFile(File myFile) {
  this.myFile = myFile;
 }

 public String getMyFileContentType() {
  return myFileContentType;
 }

 public void setMyFileContentType(String myFileContentType) {
  this.myFileContentType = myFileContentType;
 }

 public String getMyFileFileName() {
  return myFileFileName;
 }

 public void setMyFileFileName(String myFileFileName) {
  this.myFileFileName = myFileFileName;
 }

}

 

struts.xml 配置

<?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>

 <package name="default" namespace="/" extends="struts-default">
  <action name="uploadAction" class="com.struts2_upload.action.UploadAction">
   <result name="success">index.jsp</result>
  </action>
 </package>

</struts>    

 

index.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 'index.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">
 <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.js"></script>
 <script type="text/javascript" src="js/jQuery.autoIMG.js"></script>
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <script type="text/javascript">
  $(function(){
   $("#imgShow").autoIMG();//设置ID为imgShow的DIV中的图片等比例自动缩放
  });
 </script>
  </head>
  
  <body>
    
    <form action="uploadAction" method="post" enctype="multipart/form-data">
  上传图片: <input type="file" name="myFile" />
  <input type="submit" value="上传" />
 </form>
 
 <div id="imgShow" style="width:300px; height:200px; border:1px solid #FF0000;">
  <img alt="上传文件" src="${pageContext.request.contextPath}/<s:property value="'upload/'+myFileFileName"/>">
  <!-- ${pageContext.request.contextPath}获取当前项目的路径 -->
 </div>
  </body>
</html>

 
------------------------------------------------------------------------------------------------------------------------------------

详情见附件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值