struts2文件上传(使用struts2自带上传方式实现单个文件上传)

              前提:

                       1.你得项目中已经导入了使用struts2上传的jar

                       2.jar包有两个分别是commons-fileupload-1.2.1.jar         commons-io-1.3.2.jar   这两缺一不可

 

 

jsp代码如下:

          

<%@ 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 '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">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
    <form action="test/ActionTest!aiWei" method="post" enctype="multipart/form-data">
     <input type="file" name="myFile" ><br/>
     <input type="submit" value="提交">
    </form>
  </body>
</html>

 

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>
 <constant name="struts.multipart.saveDir" value="d://"/>
 <package name="test" namespace="/test" extends="struts-default">
 
   <action name="ActionTest" class="com.silveraegis.upload.ActionTest">
                 <result name="success">/success.jsp</result>
   </action>
 </package>
</struts>

 

具体实现的action:主要内容是标红代码

 

package com.silveraegis.upload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;


public class ActionTest extends ActionSupport {

 private File myFile;  //必须和页面的上传元素name属性值一样
 private String myFileFileName;//  String属性名称蓝色部分必须和File属性名称一样 区分大小写  (将要上传的文件名称)
 private String myFileContentType;//String属性名称蓝色部分必须和File属性名称一样 区分大小写 (将要上传的文件内型)

 //必须给以上三个属性提供get() set()
 

 
 
 public String aiWei() throws Exception{


  FileInputStream fis = new FileInputStream(myFile);
  BufferedInputStream bis = new BufferedInputStream(fis);
  
  FileOutputStream fos = new FileOutputStream("C://"+this.getMyFileFileName());
  BufferedOutputStream bos = new BufferedOutputStream(fos);
  
  byte[] b = new byte[1024];
  
  int len = 0;
  
  while((len=bis.read(b))!=-1){
   
   bos.write(b, 0, len);
  }
  
  bos.close();
  fos.close();
  bis.close();
  fis.close();
  
  return "success";
 }
 
 

 public File getMyFile() {
  return myFile;
 }

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

 

 public String getMyFileFileName() {
  return myFileFileName;
 }

 

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

 

 public String getMyFileContentType() {
  return myFileContentType;
 }

 

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


}

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值