carga y descarga de archivos struts2

1. Formulario de página

<HTML>
	<HEAD>
		<TITLE>上传下载图片</TITLE>
		<meta http-equiv="Content-Type" content="text/html; charset=GBK">
	</head>
	<body>
		<form enctype="multipart/form-data" action="hello/fileUploadAction!fileUpload" method="post">
			<table>
				
				<tr>
					<td>文件:</td>
					<td><input type="file" name="uploadImage"></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" value="upload">
						<input type="reset" value="重 置">
					</td>
				</tr>
			</table>
			
			<img alt="qian" src="hello/fileUploadAction!fileDownload">
		</form>
	</body>
</html>

2. Implementación de carga y descarga

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import org.apache.commons.io.FileUtils;

import base.BaseAction;

public class FileUploadAction extends BaseAction {
    
    

	private static final long serialVersionUID = 1L;
	
	// uploadImage 为表单文件名称
	private File uploadImage;
	private String uploadImageFileName; // 文件名 
	private String uploadImageContentType; // 文件类型
	
	public File getUploadImage() {
    
    
		return uploadImage;
	}


	public void setUploadImage(File uploadImage) {
    
    
		this.uploadImage = uploadImage;
	}


	public String getUploadImageFileName() {
    
    
		return uploadImageFileName;
	}


	public void setUploadImageFileName(String uploadImageFileName) {
    
    
		this.uploadImageFileName = uploadImageFileName;
	}


	public String getUploadImageContentType() {
    
    
		return uploadImageContentType;
	}


	public void setUploadImageContentType(String uploadImageContentType) {
    
    
		this.uploadImageContentType = uploadImageContentType;
	}


	public String fileUpload() {
    
    
		
		String path = "E:ssh\\struts2\\Struts_06\\fileDir";
		// 创建存储文件的文件夹
		File fileDir = new File(path);
		
		if (!fileDir.exists()) {
    
    
			fileDir.mkdirs();
		}
		
		int count = uploadImageFileName.lastIndexOf(".");
		// 得到文件后缀名
		String ext = uploadImageFileName.substring(count);
		
		UUID uuid = UUID.randomUUID();
		
		String fileName = uuid + ext;
		
		File file = new File(path + "\\" + fileName);
		
		if (!file.exists()) {
    
    
			try {
    
    
				// 创建文件
				file.createNewFile();
			} catch (IOException e) {
    
    
				e.printStackTrace();
			}
		}
		
		try {
    
    
			// 复制文件到上传的路径
			FileUtils.copyFile(uploadImage, file);
		} catch (IOException e) {
    
    
			e.printStackTrace();
		}
		
		return "index";
	}
	
	public void fileDownload() {
    
    
		File file = new File("E:ssh\\struts2\\Struts_06\\fileDir\\4fcd0f06-575a-47fc-a010-d0a232ef9947.jpg");
		
		System.out.println(file);
				
		// 清除空格
		resp.reset();
		// 设置文件 ContentType 自动判断下载类型
		resp.setContentType("multipart/form-data");
		// 设置文件头
		resp.setHeader("Content-Disposition", "attachment;fileName="+file.getName());
		
		InputStream in = null;
		OutputStream out = null;
		try {
    
    
			in = new FileInputStream(file);
			out = resp.getOutputStream();
			// 准备字节数组装 
			byte[] b = new byte[(int)file.length()];
			
			// 将文件读取到内存中
			in.read(b);
			
			// 使用response响应流 写到页面
			out.write(b);

		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			try {
    
    
				if (out != null)
					out.close();
				if (in != null)
					in.close();
			} catch (IOException e) {
    
    
				e.printStackTrace();
			}
		}
		

	}
	
	public static void main(String[] args) {
    
    
		String str = "fhh.jpg";
	
		int i = str.lastIndexOf(".");
		
		String string = str.substring(i);
		System.out.println(string);
	}

}

Supongo que te gusta

Origin blog.csdn.net/qq_44783283/article/details/108423449
Recomendado
Clasificación