UploadFileAction.java(上传文件Action)
- package com.blog.action.upload;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import com.blog.util.Constant;
- import com.blog.util.file.FileUtil;
- import com.opensymphony.xwork2.ActionSupport;
- public class UploadFileAction extends ActionSupport {
- /**
- * <param name="allowTypes">
- p_w_picpath/bmp,p_w_picpath/png,p_w_picpath/gif,p_w_picpath/jpeg
- </param>
- */
- private static final long serialVersionUID = 1L;
- /** 上传文件对象 */
- private File upload;
- /** 上传文件文件名 */
- private String uploadFileName;
- /** 上传文件文件类型 */
- private String uploadContentType;
- /** 允许上传文件类型 */
- private String allowTypes = "p_w_picpath/bmp,p_w_picpath/png,p_w_picpath/gif,p_w_picpath/jpeg";
- /**
- * 复制文件到本地目录
- *
- * @param origPath
- * @param targPath
- */
- private void copy(String savePath, String saveToDBPath){
- System.out.println("开始上传单个文件--------------------");
- System.out.println(savePath);
- System.out.println("============" + getUploadFileName());
- System.out.println("============" + getUploadContentType());
- System.out.println("============" + getUpload());
- //以服务器的文件保存地址和原文件名建立上传文件输出流
- try {
- FileOutputStream fos = new FileOutputStream(savePath + "/" + this.getUploadFileName());
- FileInputStream fis = new FileInputStream(getUpload());
- byte[] buffer = new byte[1024];
- int len = 0 ;
- while((len = fis.read(buffer)) > 0){
- fos.write(buffer, 0, len);
- }
- //关闭流
- fis.close();
- fos.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch(Exception exception){
- exception.printStackTrace();
- }
- }
- /**
- * 上传文件
- */
- public String upload(){
- FileUtil fileUtil = FileUtil.getFileUtil();
- // 文件保存路径
- String savePathRoot = Constant.UPLOAD_FILE_SAVE_ROOT + "bus/" + fileUtil.getDateFolderName();
- // 数据库保存路径
- String saveToDBRoot = "bus/" + fileUtil.getDateFolderName() + "/" + this.getUploadFileName();
- //创建该文件目录
- fileUtil.createFolder(savePathRoot);
- //开始复制文件
- this.copy(savePathRoot, saveToDBRoot);
- return SUCCESS;
- }
- public String execute(){
- return this.upload();
- }
- public File getUpload() {
- return upload;
- }
- public void setUpload(File upload) {
- this.upload = upload;
- }
- public String getUploadFileName() {
- return uploadFileName;
- }
- public void setUploadFileName(String uploadFileName) {
- this.uploadFileName = uploadFileName;
- }
- public String getUploadContentType() {
- return uploadContentType;
- }
- public void setUploadContentType(String uploadContentType) {
- this.uploadContentType = uploadContentType;
- }
- public String getAllowTypes() {
- return allowTypes;
- }
- public void setAllowTypes(String allowTypes) {
- this.allowTypes = allowTypes;
- }
- }
辅助类:FileUtil
- package com.blog.util.file;
- import java.io.File;
- import java.util.Date;
- import com.blog.util.date.DateUtil;
- public class FileUtil {
- private static FileUtil fileUtil = new FileUtil();
- private FileUtil(){
- }
- /**
- * 创建指定目录文件夹
- *
- * @param dst
- * @return boolean
- */
- public boolean createFolder(String dst){
- boolean result = false;
- File file = new File(dst);
- file.delete();//删除文件夹
- if(!file.exists()){
- result = file.mkdirs();
- }
- return result;
- }
- /**
- * 获取日期名文件夹字符串
- *
- * @return String
- */
- public String getDateFolderName(){
- DateUtil dateUtil = DateUtil.getDateUtil();
- Date date = dateUtil.getDate();
- StringBuffer sBuffer = new StringBuffer();
- sBuffer.append(String.valueOf(date.getYear()+1900));
- sBuffer.append(String.valueOf(date.getMonth() + 1));
- sBuffer.append(String.valueOf(date.getDate()));
- sBuffer.append(String.valueOf(date.getHours()));
- sBuffer.append(String.valueOf(date.getMinutes()));
- sBuffer.append(String.valueOf(date.getSeconds()));
- return sBuffer.toString();
- }
- /**
- * 获取文件工具类对象
- *
- * @return FileUtil
- */
- public static FileUtil getFileUtil() {
- return fileUtil;
- }
- }
辅助类:Constant
- package com.blog.util;
- public interface Constant {
- /** 上传文件根目录 */
- public static final String UPLOAD_FILE_SAVE_ROOT = "F:/upload/";
- }
转载于:https://blog.51cto.com/zhangmin/582106