首先要加入上传文件需要的jar文件 commons-fileupload-1.2.1.jar commomons-io-1.3.2.jar不同版本的strutsjar文件的版本也可能不同,一般在配置struts的时候已经加载了这两个文件,

然后再页面创建文件上传表单

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'FileUpload.jsp' starting page</title>
<style type="text/css">
.mylabel{
height:33px;
width: 142px;
display: block;
overflow: hidden;
background: url(image/huang_1_btn.jpg);
}
.file{
display:none;
}
#show{
width : 600px;
height: 50px;
background-color:#efef00;
font-size: 21px;
color:#00f;
font-weight: bold;
line-height: 50px;
text-align: center;
overflow: hidden;
}
#sub{
display : block;
width : 142px;
height: 33px;
background-color:#e34545;
margin-top:10px;
line-height: 33px;
text-decoration: none;
color : #fff;
}
</style>
<script type="text/javascript">
function show(value){
var $show = document.getElementById("show");
$show.innerHTML= value;
var image = document.getElementById("image");
image.src = value;
}
function sub(){
var fo = document.getElementById("form1");
fo.submit();
}
</script>
</head>

<body>
<center>
<form id="form1" action="http://localhost:8080/Struts/fileup/demo1.action" method="POST" enctype="multipart/form-data">
<label class="mylabel">
<input type="file" name="image" class="file" onchange="show(this.value);"/>
</label>
</form>
<div id="show">

</div>
<a href="javascript:sub()" id="sub">上传文件</a>
</center>
</body>
</html>

里面用到的一些图片可以自行替代

现在可以编写action 了

package com.fileupload;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class demo1 {

private File image;                     //必须与文件上传表单中的 file 的 name属性一样
private String imageFileName;    //必须是上面的文件名+FileName

public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

public String execute() throws IOException{
String realpath = ServletActionContext.getServletContext().getRealPath("/image"); //获得image的绝对路径
System.out.println(realpath);
File saveFile = new File(new File(realpath),imageFileName);   //在image文件夹下创建同名文件
FileUtils.copyFile(image, saveFile);   //将上传的文件复制给创建的同名文件
return "success";
}
}

Struts文件上传的更多相关文章

  1. struts文件上传,获取文件名和文件类型

    struts文件上传,获取文件名和文件类型   Action中还有两个属 性:uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名.文件类型.这是S ...

  2. struts文件上传(多文件)

    第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  3. struts——文件上传

    上传文件在一个系统当中是一个很常用的功能,也是一个比较重要的功能.今天我们就一起来学习一下Struts2如何上传文件. 今天讲的上传文件的方式有三种: 1,以字节为单位传输文件: 2,Struts2封 ...

  4. struts文件上传、文件下载

    文件上传 如果在表单中上传文件,表单的enctype属性为multipart/form-data struts默认上传文件大小为2M,如果需要修改,在配置文件中设置 <constant name ...

  5. Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”

    Struts的文件上传问题,相信很多人都会使用allowedTypes参数来配置允许上传的文件类型,如下. <param name="allowedTypes"> im ...

  6. struts文件上传(单文件)

    第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  7. Struts文件上传机制

    1首先建立文件上传jsp页面如下 <form action="" method="post" enctype="multipart/form-d ...

  8. struts 文件上传下载

    上传 1.编写上传action类 UploadAction.java package jxf.b_upload; import java.io.File; import java.io.IOExcep ...

  9. struts文件上传拦截器中参数的配置(maximumSize,allowedTypes ,allowedExtensions)问题

    <interceptor-ref name="fileUpload"> <param name="allowedTypes">image ...

  10. Java Struts文件上传和下载详解

    Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...

随机推荐

  1. 【转】gcc/g++ 如何支持c11 / c++11标准编译

     如果用命令 g++ -g -Wall main.cpp  编译以下代码 : 1 2 3 4 5 6 7 8 9 10 11 12 /*     file : main.cpp */ #include ...

  2. html使用css让文字多行超出部分用省略号三个点显示的方法案例

    text-overflow: -o-ellipsis-lastline;overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-w ...

  3. Laravel默认使用utf8_unicode_ci 即使数据库是utf8_general_ci

    这样做的后果是,不方便和utf8_general_ci字符串字段进行JOIN查询ON.

  4. Linq——Count、Sum、Min、Max、Average

    using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; us ...

  5. R安装

    linux: 在编译R之前,需要通过yum安装以下几个程序: #yum install gcc-gfortran              #否则报”configure: error: No F77 ...

  6. 前端环境安装(node.js+npm+grunt+bower)

    前端开发环境安装(本教程不带开发工具的安装教程,只是环境安装) 本人机器环境win7 64位. 一.node.js安装 进入官网下载node.js文件,http://www.nodejs.org/ 2 ...

  7. E - Cup 2(dfs)

    E - Cup 2 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Stat ...

  8. Javascript 数字保留2位小数

    整理使用Javascript函数将数值保留两位小数: 1.num.toFixed(2) //进位 2.(Math.round(num * 100) / 100).toFixed(2) //进位 3.( ...

  9. linux安装bind with DLZ <NIOT>

    2015年6月11日 1.sudo wget ftp://ftp.isc.org/isc/bind9/9.10.1/bind-9.10.1.tar.gz  或者 使用“rz”命令 2.tar -zxv ...

  10. CSS-负边距原理

    一.负边距原理 正边距以相邻模块的位置为参考点进行移动,并对周围模块进行合理地排挤. 负边距即margin的四个边界值为负值. 在html中使用负边距margin-left和margin-top相当于 ...