核心提示:上传单个文件:范例: jsp代码%@ page language=java contentType=text/html; charset=UTF-8pageEncoding=UTF-8%!DOCTYP...
上传单个文件:
范例: jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://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" "https://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>