struts2框架中文件上传:
来源:互联网 发布:逍遥游2.4软件下载 编辑:程序博客网 时间:2024/06/05 02:02
定义表单:
<input type="file" name="upload" method="post" enctype="multipart/form-data"/>
定义接收表单的action属性
/* struts2框架中文件上传:* 文件的条件:* 表单提交的方式需要是POST* 表单中上传项必须有一个name属性.表单的enctype属性值需要是multipart/form-data*/// 文件上传需要的三个属性:public class FileAction{//upload必须与表单name相同 private File upload; private String uploadFileName; private String uploadContentType; public void setUpload(File upload) { this.upload = upload; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; }}
定义action接收的方法,主意在struts文件定义此action,还有web.xml中定义拦截的路径
//import org.apache.commons.io.FileUtils; public String save() throws IOException { // 将提交的数据添加到数据库中.product为接收的类 product.setPdate(new Date()); // product.setImage(image); if(upload != null){ // 将商品图片上传到服务器上. // 获得上传图片的服务器端路径. String path = ServletActionContext.getServletContext().getRealPath( "/products"); // 创建文件类型对象: File diskFile = new File(path + "//" + uploadFileName); // 文件上传: FileUtils.copyFile(upload, diskFile); product.setImage("products/" + uploadFileName); } productService.save(product); return "saveSuccess"; }
0 0
- struts2框架中文件上传:
- Struts2中文件上传
- struts2中文件上传
- struts2中文件上传
- SSH网上商城--Struts2框架中文件的上传
- struts2中文件上传路径
- Yii框架中文件上传
- yii框架中文件上传
- yii框架中文件上传
- jsp、struts1.2、struts2 中文件上传
- struts2中文件上传的实现
- jsp\struts1.2\struts2 中文件上传
- struts2中文件上传的实现
- struts2中文件上传和下载
- Struts2中文件的上传与下载
- struts2中文件上传与错误国际化
- struts2中文件上传与下载
- struts2中文件上传与下载
- 快读快写
- hdu 5475 An easy problem
- 超星视频讲座笔记(2014-3-19,4-14,4-16,4-17,4-22,4-26)
- HDU-5211 (aF(i) mod ai = 0)
- html按 字母排序
- struts2框架中文件上传:
- 冒泡排序 Java
- qt信号和槽
- 三国游戏_洛谷1199_博弈
- 简单的定时中断(1s)
- 概率论
- Java中ArrayList类的用法(转)
- hdu 1874 畅通工程续 floyd
- 富文本之BootStrap-wysiwyg