利用struts实现文件上传和下载

在配置文件struts.xml中:

 

<!-- 文件上传 -->
		<action name="file" class="com.kaishengit.action.FileAction">
			<result>/WEB-INF/views/upload.jsp</result>
		</action>
		<action name="upload" class="com.kaishengit.action.FileAction" method="upload">
			<result type="redirectAction">
				<param name="actionName">file</param>
			</result>
		</action>
		
		<!-- 文件下载 -->
		<action name="download" class="com.kaishengit.action.DownloadAction">
			<result type="stream">
				<param name="contentType">${type}</param>// 文件类型
				<param name="contentLength">${length}</param>//文件大小
				<param name="contentDisposition">attachment;filename="${fileName}"</param>//文件名
				<param name="inputName">file</param>//在接受的action中必须要有对应的方法;
				<param name="bufferSize">1024</param>//缓存区大小
				<param name="allowCaching">true</param>// 是否需要缓存
				<param name="contentCharSet">UTF-8</param>
			</result>
		</action>

 

 

在action中,这里我写的是多个文件上传的;

 

上传的FileAction

 

 

package com.kaishengit.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import com.opensymphony.xwork2.Action;

public class FileAction {

	private String fileDesc;
	private File[] documents;//这里的documents必须跟jsp中文件file类型的name属性一样;
	private String[] documentsFileName;//文件名必须以FileName结尾;这是java中的约定;
	private List<String> documentsContentType;
	
	public String execute() {
		return Action.SUCCESS;
	}
	
	public String upload() throws Exception {
		for(int i = 0;i < documents.length;i++) {
			File document  = documents[i];
			
			System.out.println("------------------------------------------");
			System.out.println("FileName :" + documentsFileName[i]);
			System.out.println("documentContentType:" + documentsContentType.get(i));
			System.out.println(document.getName());
			System.out.println(document.length());
			System.out.println("------------------------------------------");
			
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(document));
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:/upload/"+documentsFileName[i]));
			
			byte[] buffer = new byte[1024];
			int len = -1;
			while((len = bis.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
			}
			
			bos.flush();
			bos.close();
			bis.close();
		}
		
		
		return Action.SUCCESS;
	}


	public String getFileDesc() {
		return fileDesc;
	}


	public void setFileDesc(String fileDesc) {
		this.fileDesc = fileDesc;
	}


	public File[] getDocuments() {
		return documents;
	}


	public void setDocuments(File[] documents) {
		this.documents = documents;
	}


	public String[] getDocumentsFileName() {
		return documentsFileName;
	}


	public void setDocumentsFileName(String[] documentsFileName) {
		this.documentsFileName = documentsFileName;
	}


	public List<String> getDocumentsContentType() {
		return documentsContentType;
	}


	public void setDocumentsContentType(List<String> documentsContentType) {
		this.documentsContentType = documentsContentType;
	}
	
}

 

 

下载的action:

 

package com.kaishengit.action;

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

import com.opensymphony.xwork2.Action;

public class DownloadAction {

	private String type;
	private long length;
	private String fileName;
	
	public String execute() {
		return Action.SUCCESS;
	}
	
	public InputStream getFile() throws Exception {
		type = "application/pdf";
		fileName = new String("简历.pdf".getBytes("UTF-8"),"ISO8859-1");
		
		File file = new File("C:/upload/123.pdf");
		InputStream input = new FileInputStream(file);
		length = file.length();
		
		return input;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public long getLength() {
		return length;
	}

	public void setLength(long length) {
		this.length = length;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	
}

 

上传和下载的jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'upload.jsp' starting page</title>
  </head>
  
  <body>
    
    <form action="upload.action" method="post" enctype="multipart/form-data">
    	文件的描述:<input type="text" name="fileDesc"/><br/>
    	<input type="file" name="documents"/><br/>
    	<input type="file" name="documents"/><br/>
    	<input type="submit" value="上传"/>
    </form>

    <a href="download.action">点击下载</a>
    <br/>

  </body>
</html>
 

你可能感兴趣的:(struts,upload,download)