您的位置:首页 > 其它

文件上传和下载

2016-06-05 22:45 169 查看
//文件上传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(<
1250b
span class="n" style="color:rgb(147,161,161);">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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: