import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID; import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class uploadAction extends ActionSupport {
private File uploadpic;
private String uploadpicContentType;
private String uploadpicFileName; /**
* @return
*/
public String execute() throws Exception {
// TODO Auto-generated method stub
InputStream is = new FileInputStream(uploadpic);
String photoPath = ServletActionContext.getServletContext()
.getRealPath("/images");
File filePhotoPath = new File(photoPath);
if (!filePhotoPath.isDirectory()) {
filePhotoPath.mkdir();
} String extension = FilenameUtils.getExtension(this.getUploadpicFileName());
String filename = UUID.randomUUID().toString() + "." + extension; File tofile = new File(photoPath, filename); OutputStream os = new FileOutputStream(tofile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close(); return SUCCESS;
} public File getUploadpic() {
return uploadpic;
} public void setUploadpic(File uploadpic) {
this.uploadpic = uploadpic;
} public String getUploadpicContentType() {
return uploadpicContentType;
} public void setUploadpicContentType(String uploadpicContentType) {
this.uploadpicContentType = uploadpicContentType;
} public String getUploadpicFileName() {
return uploadpicFileName;
} public void setUploadpicFileName(String uploadpicFileName) {
this.uploadpicFileName = uploadpicFileName;
} }

版权声明:本文为博主原创文章,未经博主允许不得转载。

struts 文件上传示例的更多相关文章

  1. struts文件上传,获取文件名和文件类型

    struts文件上传,获取文件名和文件类型   Action中还有两个属 性:uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名.文件类型.这是S ...

  2. struts文件上传(多文件)

    第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  3. struts——文件上传

    上传文件在一个系统当中是一个很常用的功能,也是一个比较重要的功能.今天我们就一起来学习一下Struts2如何上传文件. 今天讲的上传文件的方式有三种: 1,以字节为单位传输文件: 2,Struts2封 ...

  4. 【转载】兼容php5,php7的cURL文件上传示例

    转载来自: http://www.huanlinna.com/2016/06/25/coding/php5-php7-upload-demo-via-curl.html https://segment ...

  5. struts文件上传、文件下载

    文件上传 如果在表单中上传文件,表单的enctype属性为multipart/form-data struts默认上传文件大小为2M,如果需要修改,在配置文件中设置 <constant name ...

  6. Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”

    Struts的文件上传问题,相信很多人都会使用allowedTypes参数来配置允许上传的文件类型,如下. <param name="allowedTypes"> im ...

  7. struts文件上传(单文件)

    第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  8. asp.net 文件上传示例整理

    ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法. 方法一:用Web控件FileUpload,上传到网站根目录.  代码如下 复制代码 ...

  9. Struts文件上传机制

    1首先建立文件上传jsp页面如下 <form action="" method="post" enctype="multipart/form-d ...

随机推荐

  1. 关于随机浏览头伪装fake-UserAgent

    使用: from fake_useragent import UserAgent ua = UserAgent() #ie浏览器的user agent print(ua.ie) Mozilla/5.0 ...

  2. PAT 天梯赛 L2-016. 愿天下有情人都是失散多年的兄妹 【BFS】

    题目链接 https://www.patest.cn/contests/gplt/L2-016 思路 用BFS 每层 遍历当代 并且查找当代是否有重复 有重复就跳出 然后 POP 并且将他们的下一代 ...

  3. 搭建SSM项目时报错(org.springframework.jdbc.CannotGetJdbcConnectionException)

    严重: Servlet.service() for servlet [SpringMVC] in context with path [/ssm] threw exception [Request p ...

  4. 20145229吴姗珊 《Java程序设计》第7周学习总结

    20145229吴姗珊 <Java程序设计>第7周学习总结 教材学习内容总结 第13章时间与日期 即使标注为GMT(格林威治时间),实际上谈到的的是UTC(Unix时间)时间. 秒的单位定 ...

  5. 剑指offer——翻转单词顺序VS左旋转字符串

    字符串的交换等,注意判断字符串的是否为NULL,以及判断边界等. #include <iostream> #include <string> using namespace s ...

  6. linux 资源管理

    1. 查看内存信息  free [root@rhel6 script]# free total used free shared buffers cached Mem: -/+ buffers/cac ...

  7. 剑指offer之 二维数组的查找

    package Problem3; public class Find { /* * 题目描述:二维数组中的查找 * 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下的顺序排 ...

  8. 同一台电脑关于多个SSH KEY管理

    运用shh -T -v git@github.com查看具体出错信息,再根据信息来调试    原文链接: http://yijiebuyi.com/blog/f18d38eb7cfee860c117d ...

  9. bzoj 3038: 上帝造题的七分钟2 线段树||hdu 4027

    3038: 上帝造题的七分钟2 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1066  Solved: 476[Submit][Status][Dis ...

  10. scanf ---------未完待续

    1.不可读入空格 #include<iostream> #include<stdio.h> using namespace std; int main() { char c[5 ...