Struts文件上传
首先要加入上传文件需要的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文件上传的更多相关文章
- struts文件上传,获取文件名和文件类型
struts文件上传,获取文件名和文件类型 Action中还有两个属 性:uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名.文件类型.这是S ...
- struts文件上传(多文件)
第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
- struts——文件上传
上传文件在一个系统当中是一个很常用的功能,也是一个比较重要的功能.今天我们就一起来学习一下Struts2如何上传文件. 今天讲的上传文件的方式有三种: 1,以字节为单位传输文件: 2,Struts2封 ...
- struts文件上传、文件下载
文件上传 如果在表单中上传文件,表单的enctype属性为multipart/form-data struts默认上传文件大小为2M,如果需要修改,在配置文件中设置 <constant name ...
- Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”
Struts的文件上传问题,相信很多人都会使用allowedTypes参数来配置允许上传的文件类型,如下. <param name="allowedTypes"> im ...
- struts文件上传(单文件)
第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
- Struts文件上传机制
1首先建立文件上传jsp页面如下 <form action="" method="post" enctype="multipart/form-d ...
- struts 文件上传下载
上传 1.编写上传action类 UploadAction.java package jxf.b_upload; import java.io.File; import java.io.IOExcep ...
- struts文件上传拦截器中参数的配置(maximumSize,allowedTypes ,allowedExtensions)问题
<interceptor-ref name="fileUpload"> <param name="allowedTypes">image ...
- Java Struts文件上传和下载详解
Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...
随机推荐
- angularjs 中的iframe 标签 ng-src 路径
如果直接写路径到iframe标签里的ng-src中会出现报错: 解决方法: 1.ng里面有个属性是专门用来解决跨域问题的 $sce. 用法: $scope.someUrl = $sce.trustAs ...
- makefile文件知识点记录
1赋值 = 是最基本的赋值:= 是覆盖之前的值?= 是如果没有被赋值 += 是添加等号后面的值 1.“=” make会将整个makefile展开后,再决定变量的值.也就是说,变量的值将会是整个make ...
- CoreJavaE10V1P3.3 第3章 Java的基本编程结构-3.3 数据类型
3.3 数据类型 这里所说的数据类型是指 Java的8中基本数据类型,是原生就存在的. 不同进制数的字面值表示方法 进制 字面值表示方法 例子 是否默认 JDK版本支持 2进制 0b或0B前缀(每4位 ...
- 【ARM】S5PV210芯片中的BL0的作用
S5PV210芯片中的BL0的作用:(1)关闭看门狗:(2)清除指令寄存器:(3)初始化栈区域:(4)初始化堆区域:(5)初始化块设备复制功能:(6)初始化PLL和设置系统时钟:(7)拷贝BL1到片内 ...
- nyoj 523 双向广搜
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...
- ubuntu搭建git服务器
1.准备两台ubuntu虚拟机,其中一个作为server,另一个作为client.检查是否安装ssh,如果没有安装:sudo apt-get install openssh-server, 然后在se ...
- background-size (设置背景图片的大小)
设置背景图片的大小,以长度值或百分比显示(数值包括 长度length和百分比percentage),还可以通过cover和contain来对图片进行伸缩. 语法:background-size: au ...
- robotium如何定位控件?
search类获取当前所有的view,然后根据类型或者文本去筛选,找到view后获取坐标,然后点击坐标.本质都是通过坐标点击.solo.clickonScreen方法,底层调用MotionEvent类 ...
- hql中的in查询
public List<ShopDianpu> findbymiaosha(long id, List<Object> list) throws Exception { Str ...
- SQL Server 2008新特性——更改跟踪
在大型的数据库应用中,经常会遇到部分数据的脱机和多个数据库的合并问题.比如现在有一个全省范围使用的应用程序,每个市都部署了单独的相同的应用程序服务器和数据库服务器,每个月需要将全省所有市的数据全部汇总 ...