Struts文件上传详解编程语言
2023-06-13 09:11:46 时间
用两个包:一个是commons-fileupload-1.3.2.jar,另一个是commons-io-2.2.jar
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" html head meta http-equiv="Content-Type" content="text/html; charset=utf-8" title Insert title here /title /head body form action="Shangchuan_add" method="post" enctype="multipart/form-data" input type="file" name="file" br br input type="submit" value="submit" /form /body /html
做Action。
三个成员:1.File file上传的文件 2.String fileFileName属性名 3.String fileContentType属性名
org.apache.commons.io.FileUtils.copyFile很简单。
sturts.xml配置文件,可在里面添加常量,修改文件上传的大小限制。就可以上传大文件了
?xml version="1.0" encoding="UTF-8" ? !DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" struts constant name="struts.enable.DynamicMethodInvocation" value="true" / constant name="struts.devMode" value="true" / constant name="struts.i18n.encoding" value="UTF-8" /constant constant name="struts.ui.theme" value="simple" /constant constant name="struts.multipart.maxSize" value="20971520" /constant !-- 修改限制上传文件大小,默认是2097152 -- package name="default" namespace="/" extends="struts-default" action name="*_*" method="{2}" result {1}_{2}.jsp /result /action /package include file="example.xml"/ !-- Add packages here -- /struts
解决重命名问题
package com.itnba.maya.controller; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class ShangchuanAction extends ActionSupport { private File file; private String fileFileName;//上传文件的名字 private String fileContentType;//上传文件的格式
public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName;
public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType;
public String add() throws IOException{ //为了防止上传文件重名,在文件前面加上当前时间 long l=System.currentTimeMillis(); Date date=new Date(l); SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddhhmmssSSS");//精确到毫秒 String pre=sdf.format(date); String fileName = pre+fileFileName; File destFile = new File("d://"+fileName);//放到哪里的路径+文件名 FileUtils.copyFile(file, destFile); return SUCCESS; }
上传完成
*************************************************************************************
多个文件同时上传,Action类里面三个成员定义成数组,用数组来接收
package com.itnba.maya.controller; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class ShangchuanduoAction extends ActionSupport { private File[] file; private String[] fileFileName; private String[] fileContentType; public File[] getFile() { return file; public void setFile(File[] file) { this.file = file; public String[] getFileFileName() { return fileFileName; public void setFileFileName(String[] fileFileName) { this.fileFileName = fileFileName; public String[] getFileContentType() { return fileContentType; public void setFileContentType(String[] fileContentType) { this.fileContentType = fileContentType; public String input(){ return SUCCESS; public String add() throws IOException{ //遍历数组 for(int i=0;i file.length;i++){ File item= file[i]; //取出每个File if(item!=null){ File temp = new File("d://"+fileFileName[i]);//去出每个的fileFileName FileUtils.copyFile(item, temp); return SUCCESS; }
jap页面:
%@ 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" html head meta http-equiv="Content-Type" content="text/html; charset=utf-8" title Insert title here /title /head body h1 多文件上传 /h1 form action="Shangchuanduo_add" method="post" enctype="multipart/form-data" 附件1: input type="file" name="file" br 附件2: input type="file" name="file" br 附件3: input type="file" name="file" br 附件4: input type="file" name="file" br 附件5: input type="file" name="file" br br input type="submit" value="submit" /form /body /html
上传成功
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16994.html
cjavaxml相关文章
- Springboot上传文件到Linux服务器
- ASP.NET MVC 上传文件方法
- Spring MVC框架:第九章:文件上传
- 上传文件到ftp服务器java代码详解编程语言
- Java 利用HttpClient进行http文件上传详解编程语言
- Java使用commons-net实现FTP文件上传详解编程语言
- struts2 文件上传Action类详解编程语言
- 基于spring的文件上传–单文件上传详解编程语言
- SpringMvc多文件上传简单实现详解编程语言
- springMVC从上传的Excel文件中读取数据详解编程语言
- ABAP_DEBUG时通过文件上传数据到内表详解编程语言
- 限制解决MySQL文件上传大小的限制(mysql文件上传大小)
- Linux下文件上传的路径探索(linux上传文件路径)
- 上传Linux服务器上JSP文件的快速上传(linuxjsp文件)
- 新手上路 | 上传Word文件形成存储型XSS路径
- Asp.netFCKEditor2.6.3上传文件没有权限解决方法
- asp.net中上传图片文件实现防伪图片水印并写入数据库
- Easy.Ajax部分源代码支持文件上传功能,兼容所有主流浏览器
- struts2中实现多个文件同时上传代码