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类进行处理或移动到一个永久的 ...
随机推荐
- VS辅助工具
tfs签入策略修改工具Team Foundation Server 2015 Power Tools(tfpt) 添加签入禁止策略 \*/bin \*/obj \*/Release \*/Debug ...
- SSH通过超链接传递中文参数出现乱码问题
通过超链接传递中文参数出现乱码问题 tomcat中的server.xml文件中修改如下配置: <Connector port="8080" protocol="HT ...
- java操作mongodb——插入数据
在mongodb中,表(Table)被称之为集合(Collection),记录(Record)被称为文档(Document) 首先连接到数据库 MongoClient mongoClient = ne ...
- mysql的数据类型与列属性
- MySQL新增用户以及数据库访问授权
# mysql -u root -p # 允许本地 IP 访问 localhost, 127.0.0.1 # insert into mysql.user(Host,User,Password) va ...
- Hbase压力测试
PerformanceEvaluation是HBase自带的性能测试工具,该工具提供了顺序读写.随机读写.扫描等性能测试功能.本文简要介绍HBase PerformanceEvaluation的使用方 ...
- Python学习笔记——基础篇【第一周】——变量与赋值、用户交互、条件判断、循环控制、数据类型、文本操作
目录 Python第一周笔记 1.学习Python目的 2.Python简史介绍 3.Python3特性 4.Hello World程序 5.变量与赋值 6.用户交互 7.条件判断与缩进 8.循环控制 ...
- CNN-梯度下降
一.梯度检测: 对于函数而言通常有两种计算梯度的方式: 1.数值梯度 (numberical gradient) 2.解析梯度 (analytic gradient ) 数值梯度计算通常为: 更为常见 ...
- sqlalchemy相关知识
#!/usr/bin/env python3.5 # -*- coding:utf8 -*- # 多对多关联 from sqlalchemy import Table, Column, Integer ...
- Textarea自适应高度 JS实现,兼容IE6\7\8\9\10\11
<!DOCTYPE html> <html> <head> <title>autoresizing textarea</title> < ...