- 浏览: 133899 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
xiaojiit:
99九夜茴 写道请问能不能把服务器端是Java的Web传我一下 ...
C# 编程WinForm 上传文件到 Asp.Net 的 Web项目 -
99九夜茴:
请问能不能把服务器端是Java的Web传我一下啊?我把代码复制 ...
C# 编程WinForm 上传文件到 Asp.Net 的 Web项目 -
xiaojiit:
不好意思,忘记传了。
Web通用简体繁体转化Js -
wwwcomy:
Language.js他人呢
Web通用简体繁体转化Js -
xiaojiit:
luogen33 写道 这美眉 很强悍啊!!哈哈,过奖过 ...
Java 获取系统信息,包括CPU使用率、硬盘大小、网卡状态、系统信息等
Struts的文件上传
本文用的是Struts1.1的org.apache.struts.upload.FormFile类。很方便,不用自己写。也不用写一个jsp调用jspsmartupload就可以搞定。
选择上传文件页面:selfile.jsp
--------------------------------------------------------------------------------
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<html:form action="/uploadsAction.do" enctype="multipart/form-data">
<html:file property="theFile"/>
<html:submit/>
</html:form>
</html:html>
--------------------------------------------------------------------------------
UpLoadAction.java
--------------------------------------------------------------------------------
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
public class UpLoadAction extends Action {
<wbr> </wbr><wbr>public ActionForward execute(ActionMapping mapping,
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> ActionForm form,
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> HttpServletRequest request,
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> HttpServletResponse response)
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>throws Exception {
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>if (form instanceof uploadsForm) {//如果form是uploadsForm
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>String encoding = request.getCharacterEncoding();
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>{
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>UpLoadForm theForm = (UpLoadForm ) form;
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>FormFile file = theForm.getTheFile();//取得上传的文件
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>try {
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>InputStream stream = file.getInputStream();//把文件读入
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>String filePath = request.getRealPath("/");//取当前系统路径
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>ByteArrayOutputStream baos = new ByteArrayOutputStream();
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>OutputStream bos = new FileOutputStream(filePath + "/" +
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>file.getFileName());//建立一个上传文件的输出流
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>//System.out.println(filePath+"/"+file.getFileName());
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>int bytesRead = 0;
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>byte[] buffer = new byte[8192];
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>bos.write(buffer, 0, bytesRead);//将文件写入服务器
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>bos.close();
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>stream.close();
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}catch(Exception e){
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>System.err.print(e);
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>//request.setAttribute("dat",file.getFileName());
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>return mapping.findForward("display");
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>return null;
</wbr><wbr> </wbr><wbr>}
}
--------------------------------------------------------------------------------
UpLoadForm.java
--------------------------------------------------------------------------------
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
public class UpLoadForm extends ActionForm {
</wbr><wbr> </wbr><wbr>public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
</wbr><wbr> </wbr><wbr>protected FormFile theFile;
</wbr><wbr> </wbr><wbr>public FormFile getTheFile() {
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>return theFile;
</wbr><wbr> </wbr><wbr>}
</wbr><wbr> </wbr><wbr>public void setTheFile(FormFile theFile) {
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>this.theFile = theFile;
</wbr><wbr> </wbr><wbr>}
</wbr><wbr> </wbr><wbr>public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
</wbr><wbr> </wbr><wbr>{
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>ActionErrors errors = null;
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>//has the maximum length been exceeded?
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>Boolean maxLengthExceeded = (Boolean)
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>{
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>errors = new ActionErrors();
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError("maxLengthExceeded"));
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>return errors;
</wbr><wbr> </wbr><wbr>}
}
//这是相对应的form,还有其他属性可以设置,具体可以参考struts的上传例子。
--------------------------------------------------------------------------------
struts-config.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
</wbr><wbr> </wbr><wbr><form-beans>
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr><form-bean name="uploadsForm" type="UpLoadForm" />
</wbr><wbr> </wbr><wbr></form-beans>
</wbr><wbr> </wbr><wbr><action-mappings>
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr><action name="uploadsForm" type="UpLoadAction" path="/uploadsAction">
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr><forward name="display" path="/display.jsp" />
</wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></action>
</wbr><wbr> </wbr><wbr></action-mappings>
</struts-config>
<!--display.jsp就是随便写一个成功页-->
发表评论
-
【小程序】java生成integer的字符串序列
2016-08-06 11:33 4711、效果如下: 输入1——5,返回1,2,3, ... -
Java单线程池
2015-07-22 10:04 608package com.hebei.read.core.pl ... -
自定义Java多线程池
2015-07-22 10:04 554package com.anda.threads; i ... -
自定义Java线程池
2015-07-22 10:02 0package com.anda.threads; i ... -
Linux下将Tomcat设置为Chkconfig启动方式
2013-12-14 16:19 732#!/bin/sh # # tomcat: Start ... -
java文件下载文件名乱码(转)
2013-11-19 11:25 739response.setHeader(...)下载 ... -
Tomcat、Apache、Mod_JK整合
2012-01-16 19:17 1550Apache、Tomcat、mod_jk 域名解析 1、安装 ... -
Java HashMap的两种排序方式
2011-12-09 18:31 944Map<String, Integer> ... -
使网页变灰,支持IE、FireFox、Opera、360浏览器等
2011-12-06 10:44 28911、对IE的支持,只需要在CSS中添加如下代码: ... -
Java 获取指定时间
2011-12-05 17:29 2144// 用来全局控制 上一周,本周,下一周的周数变化 ... -
Java 获取系统信息,包括CPU使用率、硬盘大小、网卡状态、系统信息等
2011-10-28 15:05 8847采用singar.jar实现,需要将sigar-x86-win ... -
CentOS Linux 下Tomcat启动脚本
2011-09-24 10:09 1587#!/bin/bash # # Init file ... -
IE6下DIV高度的问题
2011-06-30 09:14 874在IE6下,使用 height:10px;时,比实际的高度要高 ... -
非常不错的JS插件
2010-04-22 10:26 687从网上发现一个JS日期插件,经过自己修改,非常好用了,在此标识 ... -
基于xfire的web service搭建
2009-03-27 12:39 1659这里,用的xfire1.2.6和Spring 2.0,开发工具 ... -
Linux系统下JSP服务器配置步骤详解
2008-03-07 10:27 998接触JSP服务器 的人基本上都是从JSP的安装开始的 ... -
Java专业术语标准化规范
2008-03-08 09:32 891-A- AJAX: (建议不译,原因:专有名词) ... -
Eclipse开发经典教程:SWT布局(1)
2008-03-08 09:52 2438在Java 中,GUI程序开发的目标之一是跨平台,而每种类型 ... -
Eclipse开发经典教程:SWT布局(2)
2008-03-08 09:54 2603GridLayout布局 GridLayout ... -
JSP 文件上传
2008-08-18 17:22 1115这些天做JSP 的网站,遇到一个上传文件的问题,经过查找,终 ...
相关推荐
在这个"Struts文件上传示例"中,我们将深入探讨如何在Struts框架下实现文件上传功能,以及相关的关键知识点。 首先,理解文件上传的基本流程至关重要。在Web应用中,用户通过表单选择本地文件,然后提交到服务器。...
JavaEE Struts 文件上传是Web开发中的一个常见需求,它允许用户从客户端向服务器传输文件。Struts作为JavaEE框架的一部分,提供了强大的功能来处理这种交互。以下是对这个主题的详细解析。 首先,理解文件上传的...
Struts 提供了强大的支持来处理文件上传操作。下面将详细介绍Struts 实现文件上传的相关知识点。 ### 1. Struts 框架概述 Struts 是Apache软件基金会下的一个开源项目,其主要目标是提供一个可扩展且易于维护的...
本篇文章将详细探讨Struts文件上传大小限制的问题。 首先,我们需要理解文件上传的基本流程。在Struts中,当用户通过表单提交文件时,数据会被封装到`ActionForm`对象中,然后由Struts的控制器组件处理。默认情况下...
### Struts文件上传知识点 #### 一、Struts框架简介 Struts是一个开源的MVC(Model-View-Controller)框架,它简化了基于Java的Web应用程序开发过程。Struits框架提供了一种结构化的方式来组织代码,使得开发更加...
在本文中,我们将深入探讨如何使用JSP(JavaServer Pages)和Struts框架实现文件上传功能。这是一个在Web开发中常见的需求,特别是在处理用户提交的数据时。Struts是一个基于MVC(Model-View-Controller)设计模式的...
Apache Commons FileUpload库是Struts文件上传的核心,它提供了处理`multipart/form-data`请求的能力。你需要在项目中引入这个库,并在Action中使用`FileItemFactory`和`ServletFileUpload`来解析请求中的文件项。 ...
一、Struts文件上传大小限制的配置 1. **struts-default.xml配置** 在Struts2框架中,我们可以通过修改`struts-default.xml`或自定义的配置文件来设置文件上传的最大大小。在`<constant>`标签中,可以设置以下两个...
"Struts文件上传下载"是Struts框架中的一个重要功能,允许用户通过Web界面上传文件到服务器,同时也支持从服务器下载文件到用户的本地设备。在实际的业务场景中,例如在线文档共享、图片分享、文件管理等,文件上传...
Struts文件上传是Java Web开发中的一个重要功能,它允许用户通过Web界面上传文件到服务器。在Struts框架中实现文件上传,主要涉及到Struts的ActionForm、Servlet API以及相关的配置文件。下面将详细介绍Struts文件...
在本项目中,"Struts文件上传和Excel导出"涵盖了两个关键功能:文件上传和数据导出到Excel。这里我们将深入探讨这两个主题。 **文件上传** 1. **表单设计**:在HTML页面上,我们需要一个`<input type="file">`标签...
Struts文件上传是一个在Java Web开发中常见的功能,主要用于允许用户通过Web应用程序上传文件到服务器。Struts是Apache软件基金会的Jakarta项目中的一个开源框架,它基于Model-View-Controller(MVC)架构模式,旨在...
首先,我们来了解一下Struts文件上传的基本步骤: 1. **配置struts.xml**:在Struts配置文件中,你需要定义一个Action,这个Action将处理文件上传请求。通过`<action>`标签设置`executeResult`方法,以及与之关联的...
### Struts 文件上传下载程序详解 #### 一、概述 Struts 是一款开源的MVC框架,被广泛应用于Java Web应用程序开发中。本篇将基于一个具体的`struts`文件上传下载程序进行深入剖析,旨在帮助读者理解Struts在文件...
总结,Struts文件上传实例展示了如何在Java Web应用中使用Struts框架实现文件上传功能。这个过程涉及Struts配置、表单Bean、Action类以及服务器端的文件处理。理解和掌握这个实例,可以帮助开发者更好地应对实际项目...
这个"struts文件上传例子"很可能是展示如何在Struts应用中处理文件上传的一个示例项目。 首先,你需要在JSP页面上创建一个`<input type="file">`元素,让用户选择要上传的文件。然后,将该表单提交到一个对应的...
在这个"struts文件上传小项目"中,我们将探讨如何利用Struts框架实现文件上传功能,这是一个常见的Web应用程序需求。 首先,我们需要了解Struts的配置文件`struts-config.xml`。在这个项目中,它会定义一个或多个...