struts上传

使用struts2 编写上传代码

(一)编写UploadAction类

package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {
private String title;              //标题
private File upload;               //上传的文件
private String uploadContentType;  //文件的类型
private String uploadFileName;     //上传文件的名称
private String savePath;          //要保存的路径
 


@Override
public String execute() throws Exception {
      //以服务器文件保存地址和源文件建立上传输出流(到哪里去)
	 FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName());
	 //以上传文件建立文件上传流(从哪里来)
	 FileInputStream fis=new FileInputStream(this.getUpload());
	 //将上传文件写到服务器
	 byte [] buffer =new byte[1024];
	 int len=0;
	 while((len=fis.read(buffer))>0)
	 {
		 fos.write(buffer,0,len);
	 }
	 fos.close();// 注意:流应当关闭。    
	 fis.close();
	 return SUCCESS;
}

public String getTitle() {
	return title;
}
public void setTitle(String title) {
	this.title = title;
}
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 ServletActionContext.getServletContext().getRealPath(savePath);
}
//注入struts配置的属性
@SuppressWarnings("deprecation")
public void setSavePath(String savePath) {
	this.savePath = savePath;
}
 }
 

 (二)在struts.xml中写入:

  <action name="upload" class="com.action.UploadAction" method="execute">
	  <!--  配置fileUpload 拦截器-->
	  <interceptor-ref name="fileUpload">
	    <param  name="allowedTypes">
	       image/bmp,image/png,image/gif,image/jpeg,image/jpg  
	    </param>
	     <!-- 配置上传文件的大小 -->
        <param name="maximumSize">500000</param>
       </interceptor-ref>
     <!-- 配置系统默认拦截器 -->
     <interceptor-ref name="defaultStack"/>
     <!-- 动态设置Action 的属性    上传文件的默认保存路径 -->
     <param name="savePath">/upload</param>
     <result name="success">/succ.jsp</result>
	  </action>

 

(三) 编写上传页面 upload.jsp

 <body>
   <form action="upload.action" method="post" enctype="multipart/form-data">
        文件标题:<input type="text" name="title"/><br/>
         选择文件:<input type="file" name="upload"/><br/>
    <input type="submit" value="上传"/>
   </form>
  </body>

 (四)编写显示页面 succ.jsp 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'success.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">
  </head>
  
  <body> 
    文件标题:<s:property value=" + title"/> 
    文件为:<img src="<s:property value="'upload/'+uploadFileName"/>"/><br/>
 HELLO NIHAO</body>
</html>
 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值