struts2文件操作

阅读更多

一、文件上传

struts2文件操作_第1张图片

fileupload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>




Insert title here





文件:

success.jsp提示上传成功


文件上传成功!

FileUploadAction.jsp

package com.java.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	private File fengjing; // 文件
	
	private String fengjingFileName;  // 文件名
	
	private String fengjingContentType;  // 文件类型

	
	@Override
	public String execute() throws Exception {
		System.out.println("文件名:"+fengjingFileName);
		System.out.println("文件类型:"+fengjingContentType);
		String filePath="D:/"+fengjingFileName;
		//创建了一个saveFile对象,路径是filePath
		//然后就可以调用这个对象的相关方法完成文件创建,删除,读取,写入等操作 
		File saveFile=new File(filePath);
		//FileUtils.copyFile(file1,file2);
		//file1,file2都是文件类型File;
		//把file1拷贝到file2 
		//FileUtils.copyFile(取到的文件, 新生成的文件);
		FileUtils.copyFile(fengjing, saveFile);
		return SUCCESS;
	}
	
	
	public File getfengjing() {
		return fengjing;
	}

	public void setfengjing(File fengjing) {
		this.fengjing = fengjing;
	}

	public String getfengjingFileName() {
		return fengjingFileName;
	}

	public void setfengjingFileName(String fengjingFileName) {
		this.fengjingFileName = fengjingFileName;
	}

	public String getfengjingContentType() {
		return fengjingContentType;
	}

	public void setfengjingContentType(String fengjingContentType) {
		this.fengjingContentType = fengjingContentType;
	}


}

struts.xml





	
		
		
			/success.jsp
		
		
	

 

结果截图:

struts2文件操作_第2张图片

struts2文件操作_第3张图片

 

48669c5d11adff9f3d495225c4f92775f27.jpg

struts2文件操作_第4张图片

 

 

二、配置文件类型和大小

struts.xml





          
	

	
		
		
			/success.jsp
              
                        /fileupload.jsp
		

			

		
	

三、多个文件上传

struts2文件操作_第5张图片

filesupload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>




Insert title here



文件1:
文件2:
文件3:

FilesUploadAction

package com.java.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FilesUploadAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	private File[] fengjing; // 文件
	
	private String[] fengjingFileName;  // 文件名
	
	private String[] fengjingContentType;  // 文件类型

	
	@Override
	public String execute() throws Exception {
		for(int i= 0;i

 

struts.xml






	
	
	
		
		
			/success.jsp
			/filesupload.jsp
		
		
	

结果:

struts2文件操作_第6张图片

struts2文件操作_第7张图片

struts2文件操作_第8张图片

 

四、文件下载

struts2文件操作_第9张图片

FileDownloadAction.java

package com.java.action;

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

import com.opensymphony.xwork2.ActionSupport;

public class FileDownloadAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String fileName;

	public String getFileName() throws Exception{
		//转码,不然下载的文件名是乱码
		fileName=new String(fileName.getBytes(),"ISO8859-1");
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public InputStream getInputStream()throws Exception{
		//下载D盘的风景1图片
		File file=new File("D:/风景1.jpg");
		//给当前的文件名赋值
		this.fileName="风景1号.jpg";
		return new FileInputStream(file);
	}

}

filedownload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>




Insert title here


文件下载

struts.xml






	
	
	
		
		
		
		
			
				attachment;filename=${fileName}
			
		
		
	

结果

struts2文件操作_第10张图片

struts2文件操作_第11张图片

转载于:https://my.oschina.net/u/3848699/blog/2209593

你可能感兴趣的:(struts2文件操作)