国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Struts文件上傳

2019-11-14 23:29:56
字體:
來源:轉載
供稿:網友
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; //必須是上面的文件名+FileNamepublic 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";}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 运城市| 雅安市| 久治县| 定日县| 泗洪县| 巴林右旗| 凤阳县| 无锡市| 大兴区| 博白县| 拜泉县| 弥勒县| 东丰县| 溧阳市| 新竹市| 五指山市| 民和| 定边县| 桂平市| 巢湖市| 宜宾县| 调兵山市| 新野县| 鄂伦春自治旗| 双鸭山市| 保康县| 大悟县| 彭山县| 蒲江县| 商南县| 鄱阳县| 蕲春县| 始兴县| 福清市| 抚顺市| 通渭县| 五寨县| 伊金霍洛旗| 屏边| 东光县| 镇赉县|