文件上传和下载

本文介绍了一个基于Struts2框架实现的简单文件上传和下载功能。通过配置拦截器来限定上传文件类型及大小,并使用自定义Action处理文件的上传与下载过程。该示例展示了如何设置上传路径、读取并保存文件,以及如何配置Struts2进行文件下载。
//文件上传
package com.bochy.action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

//上转文件
private File image;
//
private String imageFileName;
//
private String imageContentType;
//
private String savePath;
public File getImage() {
return image;
}

public void setImage(File image) {
this.image = image;
}

public String getImageFileName() {
return imageFileName;
}

public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

public String getImageContentType() {
return imageContentType;
}

public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}

public String getSavePath() {
return savePath;
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

public String execute() throws Exception {
//指定上转文件路径
String path=ServletActionContext.getServletContext().getRealPath("/img");
savePath="img/"+this.getImageFileName();
//System.out.println("path="+path);
//System.out.println("savePath="+savePath);
InputStream is=new FileInputStream(image);
File tofile=new File(path,this.getImageFileName());
OutputStream os=new FileOutputStream(tofile);
byte[] buffer=new byte[1024];
int len=0;
while((len=is.read(buffer))>0){
os.write(buffer, 0, len);
}
is.close();
os.close();
return SUCCESS;
}
}
//文件下载
package com.bochy.action;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownAction extends ActionSupport{
private String inputPath="img/c2cec3fdfc0392454814f8188494a4c27d1e2507.jpg";

public InputStream getTargetFile(){
InputStream is=ServletActionContext.getServletContext().getResourceAsStream(inputPath);
return is;
}
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}

//Struts文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="file" extends="struts-default">
<action name="fileaction" class="com.bochy.action.UploadAction">
<result name="success">/success.jsp</result>
<result name="input">/fileupordown.jsp</result>
<interceptor-ref name="fileUpload">
<param name="alloweTypes">image/bmp,image/jpg,image/png,image/gif,image/jpeg</param>
<param name="maximumSize">5000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="downaction" class="com.bochy.action.DownAction">
<result name="success" type="stream">
<param name="inputPath">img/c2cec3fdfc0392454814f8188494a4c27d1e2507.jpg</param>
<param name="contentType">image/bmp,image/jpg,image/png,image/gif,image/jpeg</param>
<param name="inputName">targetFile</param>
<param name="contentDisposition">attachement;filename="mary.jpg"</param>
<param name="bufferSize">5000000</param>
</result>
</action>
</package>
</struts>

//jsp页面
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<s:form action="fileaction.action" method="post" enctype="multipart/form-data">
<s:file name="image" label="上转文件"></s:file>
<s:submit value="上转"></s:submit>
</s:form>
<a href="downaction.action">下载文件</a>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值