码迷,mamicode.com
首页 > Web开发 > 详细

SSH简单上传功能

时间:2015-09-15 01:26:20      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

Action:

  1 package com.smics.apps.smicwechat.action.upload;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileOutputStream;
  8 import java.io.IOException;
  9 import java.io.InputStream;
 10 import java.io.OutputStream;
 11 
 12 import org.springframework.util.StringUtils;
 13 
 14 import com.opensymphony.webwork.ServletActionContext;
 15 import com.smics.apps.smicwechat.action.BaseAction;
 16 import com.smics.apps.smicwechat.domain.BasicInfo;
 17 
 18 public class UploadPicture extends BaseAction {
 19 
 20     private static final int BUFFER_SIZE = 16 * 1024;
 21 
 22     private String direction;// 指向页面类型
 23 
 24     private File file;// 得到上传的文件
 25     private String fileFileName;// 得到文件的名称
 26     private String fileContentType;// 得到文件的类型
 27 
 28     private String savePath;// 保存文件的目录
 29     private boolean flag = false;
 30 
 31     public String forward() {
 32         if ("photo_gallery".equals(direction)) {
 33             return direction;
 34         } else if ("upload_picture".equals(direction)) {
 35             return direction;
 36         }
 37         return ERROR;
 38     }
 39 
 40     @Override
 41     public String execute() throws Exception {
 42         // TODO Auto-generated method stub
 43         // 根据服务器的文件保存地址和源文件名创建目录文件全路径
 44         String dstPath = ServletActionContext.getServletContext().getRealPath(this.getSavePath()) + "\\" + this.getFileFileName();
 45         String path = this.getSavePath() + "\\" + this.getFileFileName();
 46         System.out.println("上传的文件的类型:" + this.getFileContentType());
 47         System.out.println("上传的文件的绝对路径:" + dstPath);
 48         System.out.println("上传的文件的相对路径:" + path);
 49         File dstFile = new File(dstPath);
 50         copy(this.getFile(), dstFile);
 51         this.setFlag(true);
 52         return SUCCESS;
 53     }
 54 
 55     // 自己封装的一个把源文件对象复制成目标文件对象
 56     private static void copy(File src, File dst) {
 57         InputStream in = null;
 58         OutputStream out = null;
 59         try {
 60             in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
 61             out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
 62             byte[] buffer = new byte[BUFFER_SIZE];
 63             int len = 0;
 64             while ((len = in.read(buffer)) > 0) {
 65                 out.write(buffer, 0, len);
 66             }
 67         } catch (Exception e) {
 68             // TODO Auto-generated catch block
 69             e.printStackTrace();
 70         } finally {
 71             if (null != in) {
 72                 try {
 73                     in.close();
 74                 } catch (IOException e) {
 75                     // TODO Auto-generated catch block
 76                     e.printStackTrace();
 77                 }
 78             }
 79             if (null != out) {
 80                 try {
 81                     out.close();
 82                 } catch (IOException e) {
 83                     // TODO Auto-generated catch block
 84                     e.printStackTrace();
 85                 }
 86             }
 87         }
 88     }
 89 
 90 
 91     public String getFileContentType() {
 92         return fileContentType;
 93     }
 94 
 95     public String getFileFileName() {
 96         return fileFileName;
 97     }
 98 
 99     public void setFileFileName(String fileFileName) {
100         this.fileFileName = fileFileName;
101     }
102 
103     public void setFileContentType(String fileContentType) {
104         this.fileContentType = fileContentType;
105     }
106 
107     public String getSavePath() {
108         return savePath;
109     }
110 
111     public void setSavePath(String savePath) {
112         this.savePath = savePath;
113     }
114 
115     public String getDirection() {
116         return direction;
117     }
118 
119     public void setDirection(String direction) {
120         this.direction = direction;
121     }
122 
123     public File getFile() {
124         return file;
125     }
126 
127     public void setFile(File file) {
128         this.file = file;
129     }
130 
131     public boolean isFlag() {
132         return flag;
133     }
134 
135     public void setFlag(boolean flag) {
136         this.flag = flag;
137     }
138 
139 }

 

前台页面:

1 <input type="file" name="file">

 

SSH简单上传功能

标签:

原文地址:http://www.cnblogs.com/Nicholas-Cheng/p/4808918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!