SSH简单上传功能

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">
时间: 2024-12-18 05:54:13

SSH简单上传功能的相关文章

nodejs 实现简单的文件上传功能

首先需要大家看一下目录结构,然后开始一点开始我们的小demo. 文件上传总计分为三种方式: 1.通过flash,activeX等第三方插件实现文件上传功能. 2.通过html的form标签实现文件上传功能,优点:浏览器兼容好. 3.通过xhr level2的异步请求,可以百度formData对象. 这里使用2做个练习. node插件请看下package.json文件 { "name": "upload", "version": "0.1

一个简单的blog系统(三) 增加文件上传功能

1. 一个完整的博客怎么能缺少图片呢,目前上传文件的方法有三种: (1)使用Express自带的文件上传功能,不涉及数据库. (2)使用Formidable外部模块,不涉及数据库. (3)上传到MongoDB,涉及到数据库. 可以看出,第一种方式最简单,Express通过bodyParser()解析请求体,因此我们可以使用bodyParser()来实现文件的上传功能. 2.实现过程 2.1 首先打开header.ejs,在<li><a href="/post" tit

用jsp实现简单的图片上传功能(multipart/form-data形式的表单)

2008-10-11 22:07 用jsp实现简单的图片上传功能 1 先做一个页面,选择上传的图片<body>   <form action="uploadServlet" enctype="multipart/form-data" method="POST" >           selectimage: <input type="file" name="myfile"/&

nodejs+ajax实现简单的文件上传功能

app.js中引入express的第三方中间件multer实现文件上传功能. var multer = require('multer');//用express的第三方中间件 multer 实现文件上传功能. app.use(multer({//设置文件上传到的位置 dest: './public/images/upload', rename: function (fieldname, filename) { return filename; } })); 路由 app.post('/uploa

利用SSH软件上传、下载(使用sz与rz命令)

1.简述 通常,利用SSH管理远程Linux服务器时,经常需要与本地交互文件.当然,我们可以利用FTP方式,比如通过Filezilla客户端软件.不过直接使用SSH软件(SecureCRT.Xshell)自带的上传和下载功能无疑使最方便快捷的.通常SSH软件支持的文件传输协议主要有ASCII.Xmodem.Zmodem等. rz,sz是便是Linux/Unix同Windows进行ZModem文件传输的命令行工具. 首先,你的Linux端(CentOS, Ubuntu)需要安装rz.sz. 其次,

struts2中实现文件上传功能

在web项目中,文件上传.头像上传这样的功能经常是要用到的,下面就以在struts2中实现文件上传功能为例子,简单地理一下文件上传功能的编码思路. 项目目录结构 项目源代码 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:x

使用Commons FileUpLoad组件实现文件上传功能

Commons 是Apache开放的源码组织的一个java子项目,该项目主要涉及一些开发中常用的模块,如文件上传,命令行处理,数据库连接池等.FileUpLoad就是其中的一个用于处理HTTP文件上传的子项目.Commons FileUpLoad组建具有以下几个特点: 1.使用简单:Commons FileUpLoad组件可以方便的嵌入JSP文件中,在JSP文件中仅编写少量代码就可完成文件上传功能,十分方便. 2.能够全程控制上传的内容:使用Commons FileUpLoad组件提供的对象及操

[Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能

很久没有更新博客了,再不写点东西都烂了. 这次更新一个小内容,是两个插件的组合使用,实现头像上传功能. 业务需求: 头像上传功能,要对上传的文件进行剪切,且保证头像到服务器时必须是正方形的. 优化<input type="file">的显示样式,基础的样式实在太难看了. 上传的头像需要进行质量压缩跟大小裁剪,以减缓浏览器的压力. 成果预览: 使用到的技术插件 Jcrop:用于前端"裁剪"图片 bootstrap-fileinput:用于前端优化上传控件样

php实现头像预览上传功能

最近在做php第二阶段的项目,需要用到头像上传的功能 我们要完成头像上传功能,一共要写两个php页面,第一个页面我们叫做touxiang.php,第二个页面我们叫做upload.php 1.touxiang.php 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&g