欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2开发6--在struts2中实现单个文件上传

程序员文章站 2022-04-20 21:05:03
...

文件上传几乎是每个web应用程序的一个必须模块,文件上传的实现需要将表单元素属性enctype的值设置成multipart/form-data,这样使表单数据以二进制编码的方式提交。下面在struts2中提供一种上传文件的方式,供大家参考。

struts2中上传文件必须添加的的jar包:commons-fileupload-x.x.x.jar和common-io-x.x.x.jar

创建一个fileUpAction来处理要上传文件的文件,代码如下:

package cn.test.fileUp;

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 java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class fileUpAction extends ActionSupport {
 private static final int BUFFER_SIZE=60*60;
 private File upload;//封装上传文件域的属性
 private String uploadContentType;//封装上传文件的类型
 private String uploadFileName;//封装上传文件名
 private String savePath;//封装文件上传的保存路径
 /**
  *
  * @param source 源文件
  * @param target 目标文件
  * @throws IOException
  */
 private static void copy(File source,File target) throws IOException
 {
  InputStream inputStream=null;
  OutputStream outputStream = null;
  try {
   inputStream=new BufferedInputStream(new FileInputStream(source));
   outputStream=new BufferedOutputStream(new FileOutputStream(target),(int)source.length());
   byte[] buffer=new byte[BUFFER_SIZE];
   int length=0;
   while((length=inputStream.read(buffer))>0)
   {
    outputStream.write(buffer,0,length);
    
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  if(inputStream!=null)
  {
  inputStream.close();
  }
  if(outputStream!=null)
  {
  outputStream.close();
  }
 }
 public String fileUpUI()
 {
  return "fileUpUI";
 }
 @Override
 public String execute() throws Exception {
  // TODO Auto-generated method stub
  String path1=ServletActionContext.getServletContext().getRealPath(this.getSavePath());
  File f = new File(path1);
  if(!f.exists())
  {
   f.mkdirs();
  }
  String path=path1+"\\"+this.getUploadFileName();
  System.out.println(path1);
  File target=new File(path);
  copy(this.upload,target);
  return "fsuccess";
 }
 public File getUpload() {
  return upload;
 }
 public void setUpload(File upload) {
  this.upload = upload;
 }
 public String getUploadContentType() {
  return uploadContentType;
 }
 public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
 }
 
 public String getUploadFileName() {
  return uploadFileName;
 }
 public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 }
 public String getSavePath() {
  return savePath;
 }
 public void setSavePath(String savePath) {
  this.savePath = savePath;
 }
 public static int getBufferSize() {
  return BUFFER_SIZE;
 }
}
说明:在上述action中,用upload属性封装上传文件的内容,但却不能获得上传文件的类型和文件名称。因此需要使用uploadContentType来封装上传文件的类型,用uploadFileName来封装上传文件的名字,另外,配置一个savePath来封装文件保存的路径。

struts.xml文件配置如下:

 <action name="file_*" class="cn.test.fileUp.fileUpAction" method="{1}">
        <param name="savePath">/upload</param>
        <result name="fileUpUI">/WEB-INF/jsp/fileUpUI.jsp</result>
        <result name="fsuccess">/WEB-INF/jsp/fsuccess.jsp</result>
        </action>

创建fileUpUI.jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:form action="file_execute" namespace="/"   enctype="multipart/form-data">
<s:file name="upload" label="文件"></s:file>
<s:submit value="确定"></s:submit>
</s:form>
</html>

创建fsuccess.jsp代码如下:

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

<html>

文件上传成功!
</html>

部署项目,在浏览器中输入:http://localhost:8080/MyWeb/file_fileUpUI,就可以实现上传文件功能。