博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2单文件上传案例演示(一)
阅读量:7071 次
发布时间:2019-06-28

本文共 1406 字,大约阅读时间需要 4 分钟。

public class UploadAction1 extends ActionSupport implements Serializable {
private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
private String imageFileName;//   上传输入域FileName  文件名
private String imageContentType;// 上传文件的MIME类型
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String execute(){
System.out.println(imageContentType);
try {
//处理实际的上传代码
//找到存储文件的真实路径
// System.out.println(imageFileName);
ServletContext sc = ServletActionContext.getServletContext();
String storePath = sc.getRealPath("/files");
//构建输入输出流
// OutputStream out = new FileOutputStream(storePath+"\\"+imageFileName);
// InputStream in = new FileInputStream(image);
// byte b[] = new byte[1024];
// int len = -1;
// while((len=in.read(b))!=-1){
// out.write(b, 0, len);
// }
// out.close();
// in.close();
FileUtils.copyFile(image, new File(storePath,imageFileName));
ActionContext.getContext().put("message", "上传成功!");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}

转载于:https://www.cnblogs.com/toge/p/6114720.html

你可能感兴趣的文章
PHP请求页面
查看>>
【动态规划】旅行
查看>>
Git小白入门
查看>>
基于 HTML5 的 WebGL 和 VR 技术的 3D 机房数据中心可视化
查看>>
PHP,JAVA,NET 开发比较
查看>>
平方开根 - 牛顿迭代(板子整理)
查看>>
java string字符拼接符"+"的研究
查看>>
Layui表格编辑【不依赖Layui的动态table加载】
查看>>
HDU2087剪花布条(KMP)
查看>>
NOIP2018普及初赛解析
查看>>
每次访问都生成不一样sessionId
查看>>
解决Cocos2d-x编译错误: 无法打开 源 文件 "extensions/ExtensionExport.h"
查看>>
SqlServer 循环建表、删除表、更新表
查看>>
jQuery中$.extend(true,object1, object2);深拷贝对象
查看>>
圆角和倒角
查看>>
自然语言处理之维特比算法
查看>>
ubuntu12 is not in the sudoers file
查看>>
c# 生成的没用文件
查看>>
Django文件上传
查看>>
zoj 3627(贪心)
查看>>