单文件上传 博客分类: struts StrutsMyeclipseApacheJavaOS
程序员文章站
2024-03-16 09:41:10
...
上传的表单method应设为post,enctype应设为multipart/form-data --------------------------示例--------------------------------------- upload.jsp ===================== <form action="upload.do?method=upload" method="post" enctype="multipart/form-data"> select the file you wish to upload: <input type="file" name="file" /> <br /> <input type="text" name="name"/> <input type="submit" value="submit" /> <input type="reset" value="cancel" /> </form> <% if (request.getAttribute("result") != null) { out.println(request.getAttribute("result").toString()); } %> UploadForm.java ===================== /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.supersit.struts.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; /** * MyEclipse Struts * Creation date: 10-23-2007 * * XDoclet definition: * @struts.form name="uploadForm" */ public class UploadForm extends ActionForm { /* * Generated Methods */ /** * Method validate * @param mapping * @param request * @return ActionErrors */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub return null; } /** * Method reset * @param mapping * @param request */ public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub } private FormFile file; private String name; public FormFile getFile() { return file; } public void setFile(FormFile file) { this.file = file; } public String getName() { return name; } public void setName(String name) { this.name = name; } } UploadAction.java ========================== /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.supersit.struts.action; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.supersit.struts.form.UploadForm; import com.supersit.struts.util.DateTime; import com.supersit.struts.util.MD5; /** * MyEclipse Struts Creation date: 10-23-2007 * * XDoclet definition: * * @struts.action input="/upload.jsp" */ public class UploadAction extends DispatchAction { /* * Generated Methods */ /** * Method execute * * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UploadForm uploadform = (UploadForm) form; String context = this.servlet.getServletContext().getRealPath("/"); System.out.println("context="+context); org.apache.struts.upload.FormFile formfile = uploadform.getFile(); java.io.InputStream is = null; java.io.OutputStream os = null; try { is = formfile.getInputStream(); String ext=formfile.getFileName().split("\\.")[1]; String filename = new MD5().getMD5ofStr(formfile.getFileName() + new DateTime().getCurrentDate5()) + "." + ext; System.out.println("filename===" + filename); os = new FileOutputStream(context + "/photo/" + filename); byte[] bytes = new byte[8192]; while ((is.read(bytes, 0, 8192)) != -1) { os.write(bytes, 0, 8192); } if (os != null) os.close(); if (is != null) is.close(); request.setAttribute("result", "上传成功"); String name=uploadform.getName(); name=new String(name.getBytes("ISO8859-1"),"utf-8"); System.out.println("name==="+name); } catch (FileNotFoundException e) { e.printStackTrace(); request.setAttribute("result", "上传失败"); } catch (IOException e) { e.printStackTrace(); request.setAttribute("result", "上传失败"); } return mapping.findForward("upload"); } } struts-config.xml ================= <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="uploadForm" type="com.supersit.struts.form.UploadForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action name="uploadForm" attribute="uploadForm" input="/upload.jsp" path="/upload" type="com.supersit.struts.action.UploadAction" validate="false" parameter="method"> <forward name="upload" path="/upload.jsp"></forward> </action> </action-mappings> <message-resources parameter="com.supersit.struts.ApplicationResources" /> </struts-config>
上一篇: 处理百万订单遇到的神坑!Navicat 导出Excel数字变为科学计数法,并且补零问题
下一篇: Tomcat连接池的配置及调用 博客分类: others TomcatSQLSQL ServerJDBCJava
推荐阅读
-
单文件上传 博客分类: struts StrutsMyeclipseApacheJavaOS
-
java ftp上传文件 博客分类: java
-
java ftp上传文件 博客分类: java
-
本地上传文件至服务器 博客分类: java java
-
本地上传文件至服务器 博客分类: java java
-
tftp 安装 (文件共享 上传 下载) 博客分类: tftplinux tftp安装文件共享上传下载
-
最简化版 log4J.properties文件,扩展到Struts2,Spring 博客分类: apache—Logger
-
Struts2实现单文件或多文件上传功能
-
Struts2实现单文件或多文件上传功能
-
无刷新效果的jsp文件上传的实现 博客分类: JavaWeb JSP