Struts文件上传
来源:互联网 发布:淘宝网店从哪里注册 编辑:程序博客网 时间:2024/06/07 17:34
上传单个文件:
范例: jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib uri="/struts-tags" prefix="s" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action = "login.action" method="post" enctype="multipart/form-data">账号:<input type="text" name="name"><br>图片:<input type="file" name="photo"><br><input type="submit" value="提交"></form></body></html>
LoginAction代码:
package com.action;import java.io.File;public class LoginAction { private String name; private File photo; private String photoFileName; private String photoContentType; public String execute(){ System.out.println(this.photoFileName); System.out.println(this.photoContentType); return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public File getPhoto() { return photo; } public void setPhoto(File photo) { this.photo = photo; } public String getPhotoFileName() { return photoFileName; } public void setPhotoFileName(String photoFileName) { this.photoFileName = photoFileName; } public String getPhotoContentType() { return photoContentType; } public void setPhotoContentType(String photoContentType) { this.photoContentType = photoContentType; }}
此时,完成的功能,只是将文件上传到了一个临时目录中,如果要实现将文件保存到指定路径中,需要对临时目录中的文件进行操作,往往需要对临时文件进行复制操作。
将LoginAction 代码进行修改:
public String execute() throws Exception{ System.out.println(this.photoFileName); System.out.println(this.photoContentType); File destFile = new File("D:\\"+photoFileName); FileUtils.copyFile(photo, destFile); return "success"; }
通常上传路径不是指定别的盘符,而是跟着本项目去指定
File destFile = new File(ServletActionContext.getServletContext().getRealPath(“/upload/”+photoFileName));
上传多个文件,只需要将各种文件相关的属性改成数组形式:
LoginAction代码:
package com.action;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;public class LoginAction { private String name; private File[] photo; private String[] photoFileName; private String[] photoContentType; public String execute() throws Exception{ for(int i=0;i<photo.length;i++){ System.out.println(this.photoFileName); System.out.println(this.photoContentType); File destFile = new File(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName[i])); System.out.println(destFile); FileUtils.copyFile(photo[i], destFile); } return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public File[] getPhoto() { return photo; } public void setPhoto(File[] photo) { this.photo = photo; } public String[] getPhotoFileName() { return photoFileName; } public void setPhotoFileName(String[] photoFileName) { this.photoFileName = photoFileName; } public String[] getPhotoContentType() { return photoContentType; } public void setPhotoContentType(String[] photoContentType) { this.photoContentType = photoContentType; }}
index.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib uri="/struts-tags" prefix="s" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action = "login.action" method="post" enctype="multipart/form-data">账号:<input type="text" name="name"><br>图片:<input type="file" name="photo"><br>图片:<input type="file" name="photo"><br>图片:<input type="file" name="photo"><br><input type="submit" value="提交"></form></body></html>
阅读全文
0 0
- Struts的文件上传
- struts文件上传要点
- Struts的文件上传
- struts文件上传要点
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts上传文件笔记
- Struts文件上传
- Struts的文件上传
- 上传文件(struts篇)
- Struts的文件上传
- struts 多文件上传
- struts构建文件上传
- struts多文件上传
- Struts的文件上传
- 使用Struts上传文件
- struts文件上传
- JZOJ 5172. 【NOIP2017提高组模拟6.27】lllegal Motor
- Microsoft Word 2007如何导出PDF
- Android开发通过Glide显示url图片
- shell编程四剑客find及grep
- 导出时获取到状态
- Struts文件上传
- 指令与控制器之间的交互
- angularjs学习笔记(二)控制器1
- javascription基础知识【学习整理中】
- Android设计模式
- malloc\colloc
- 1002. 写出这个数 (20)
- Shell echo命令
- shadowsocks后端一键脚本