SSH注解框架批量上传文件(上传多个文件)

版权声明:有问题可联系博主QQ:15577969,大家一起相互交流和学习。 https://blog.csdn.net/qq15577969/article/details/82722375

一、JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=utf-8">
		<title>SSH注解框架批量上传文件</title>
	</head>

	<body>

		<div align="center">
			<!-- 上传文件需要设置表单属性为 method="post" enctype="multipart/form-data" -->
			<form action="Upload.html" method="post" enctype="multipart/form-data">
				<table style="border-collapse:separate; border-spacing:0px 10px;">
					<tr>
						<td>视频文件:</td>
						<td><input type="file" name="videoUpload"></td>
					</tr>
					<tr>
						<td>音频文件:</td>
						<td><input type="file" name="musicUpload"></td>
					</tr>
					<tr>
						<td>图片文件:</td>
						<td><input type="file" name="imageUpload"></td>
					</tr>
					<tr>
						<td></td>
						<td><input type="submit" value="提交" /></td>
					</tr>
				</table>
			</form>
		</div>
	</body>

</html>

二、Action(控制器)

package com.foodshare.action;

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

import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

@Controller
@ParentPackage("json-default")
public class action extends ActionSupport {

	// 上传的文件
	private File videoUpload;
	private File musicUpload;
	private File imageUpload;
	// 上传的文件后缀名
	private String videoUploadFileName;
	private String musicUploadFileName;
	private String imageUploadFileName;

	public File getVideoUpload() {
		return videoUpload;
	}

	public void setVideoUpload(File videoUpload) {
		this.videoUpload = videoUpload;
	}

	public File getMusicUpload() {
		return musicUpload;
	}

	public void setMusicUpload(File musicUpload) {
		this.musicUpload = musicUpload;
	}

	public File getImageUpload() {
		return imageUpload;
	}

	public void setImageUpload(File imageUpload) {
		this.imageUpload = imageUpload;
	}

	public String getVideoUploadFileName() {
		return videoUploadFileName;
	}

	public void setVideoUploadFileName(String videoUploadFileName) {
		this.videoUploadFileName = videoUploadFileName;
	}

	public String getMusicUploadFileName() {
		return musicUploadFileName;
	}

	public void setMusicUploadFileName(String musicUploadFileName) {
		this.musicUploadFileName = musicUploadFileName;
	}

	public String getImageUploadFileName() {
		return imageUploadFileName;
	}

	public void setImageUploadFileName(String imageUploadFileName) {
		this.imageUploadFileName = imageUploadFileName;
	}

	/**
	 * 批量上传文件
	 * 
	 * @return
	 */
	@Action(value = "upload", results = { @Result(name = "success", type = "json") })
	public String upload() {
		// 缓存数组
		byte[] buf = new byte[1024];
		// 定义接收文件的数组
		File[] shareFile = new File[3];
		String[] shareFileFileName = new String[3];
		// 给数组赋值
		shareFile[0] = videoUpload;
		shareFile[1] = musicUpload;
		shareFile[2] = imageUpload;
		shareFileFileName[0] = videoUploadFileName;
		shareFileFileName[2] = musicUploadFileName;
		shareFileFileName[1] = imageUploadFileName;

		// 新文件名数组
		String[] newName = new String[3];
		// 后缀名数组
		String[] exName = new String[3];

		// 下面是需要写入数据库的文件相对路径
		String videoPath = "";
		String musicPath = "";
		String imagePath = "";
		try {
			// 从输入流获取文件的内容信息
			String path = "";
			for (int i = 0; i < shareFile.length; i++) {
				FileInputStream fis = new FileInputStream(shareFile[i]);
				// 获取源文件的后缀名mp4 mp3 png或jpg
				exName[i] = FilenameUtils.getExtension(shareFileFileName[i]);
				// 生成新的文件名
				newName[i] = System.currentTimeMillis() + "." + exName[i];
				if (exName[i].equals("mp4")) {
					path = ServletActionContext.getServletContext().getRealPath("/upload/video/");
					videoPath = "upload/video/" + newName[i];
				} else if (exName[i].equals("mp3")) {
					path = ServletActionContext.getServletContext().getRealPath("/upload/music/");
					musicPath = "upload/video/" + newName[i];
				} else {
					path = ServletActionContext.getServletContext().getRealPath("/upload/video/image/");
					imagePath = "upload/video/" + newName[i];
				}
				System.out.println("上传文件的全路径:" + path);
				File file = new File(path);
				if (!file.exists()) {
					file.mkdirs();
				}
				// 用输出流把文件写到新的路径下
				FileOutputStream fos = new FileOutputStream(path + File.separator + newName[i]);
				// 使用缓冲读取信息并完成文件保存工作
				int length = fis.read(buf);
				while (length > 0) {
					fos.write(buf, 0, length);
					length = fis.read(buf);
				}
				// 记得要关闭流
				fis.close();
				fos.flush();
				fos.close();
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return SUCCESS;
	}

}

猜你喜欢

转载自blog.csdn.net/qq15577969/article/details/82722375