首先要加入上传文件需要的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. smbaclient

    在linux中通过smbaclient获取windows的共享文件 列出windows的共享目录 $ smbclient -L .xxx -U administrator%password 进入指定共 ...

  2. gridview动态添加行(不用datatable实现)

    GridViewRow newrow = , , DataControlRowType.Separator, DataControlRowState.Normal); TableCell[] tc=] ...

  3. 如何使用jconsole(英文)

    http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html

  4. angular-ui-bootstrap插件API - Pagination

    Pagination: 案例 <!DOCTYPE html> <html lang="en" ng-app="myApp"> <h ...

  5. intellij idea 生成UUID

    Intellij IDEA 默认没启用这个功能 需要手动设置一下 , 下面是路径 Setting->Inspections->Serialization issues->Serial ...

  6. get与post请求

    1.post请求 如果表单以Post方式提交过来的,接收时必须以Requert.Form来接收,并且表单元素必须有name属性,而Form指定的键的名称就是name属性的值 <form meth ...

  7. Android 6.0之权限管理

    安卓6.0的权限体系分为非敏感权限和敏感权限,非敏感权限默认获取,可以手动关闭. 敏感权限必须由app在运行时动态申请.而存储读写空间权限是一个敏感权限,不是一个“很正常的必须权限”. 安卓并不是想要 ...

  8. java 缓存ehcache的使用(使用方式一)

    实体要序列化 resource文件夹下建立 ehcache.xml <?xml version="1.0" encoding="UTF-8"?> & ...

  9. Vimperator技巧

    Vimperator技巧 什么是Vimperator?Firefox的一个插件,模拟vim操作. 1. 用]]打开"下一页"链接,[[打开"上一页"Vimper ...

  10. HDU 1013 Digital Roots(字符串)

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...