struts-上傳

一、創建項目
?? ?項目名稱:demoupload
二、添加jar包
?? ?commons-fileupload-1.2.2.jar
?? ?commons-io-2.0.1.jar
?? ?commons-lang3-3.1.jar
?? ?freemarker-2.3.19.jar
?? ?javassist-3.11.0.GA.jar
?? ?ognl-3.0.5.jar
?? ?struts2-core-2.3.4.1.jar
?? ?xwork-core-2.3.4.1.jar
三、在web.xml文件中配置過濾器
?? ?<?xml version="1.0" encoding="UTF-8"?>
?? ?<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
?? ??? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?? ??? ?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?
?? ??? ?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
?? ??? ?<filter>
?? ??? ??? ?<filter-name>struts2</filter-name>
?? ??? ??? ?<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
?? ??? ?</filter>
?? ??? ?<filter-mapping>
?? ??? ??? ?<filter-name>struts2</filter-name>
?? ??? ??? ?<url-pattern>/*</url-pattern>
?? ??? ?</filter-mapping>
?? ??? ?<welcome-file-list>
?? ??? ??? ?<welcome-file>index.jsp</welcome-file>
?? ??? ?</welcome-file-list>
?? ?</web-app>
四、在WebRoot目錄下創建upload目錄用于存放上傳文件
?? ?/upload
五、添加核心配置文件
?? ?1.在項目中創建conf目錄
?? ??? ?/conf
?? ?2.在conf目錄下添加配置文件
?? ??? ?配置文件名稱:struts.xml
?? ??? ?配置文件內容:
?? ??? ?<?xml version="1.0" encoding="utf-8" ?>
?? ??? ?<!DOCTYPE struts PUBLIC
?? ??? ?????????? "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
?? ??? ?????????? "http://struts.apache.org/dtds/struts-2.3.dtd">
?? ??? ? <struts>
?? ??? ? ?? ?<constant name="struts.devMode" value="true" />
?? ??? ? ?? ?<!-- 配置全局文件大小 -->
?? ??? ? ?? ?<constant name="struts.multipart.maxSize" value="21851335"></constant>
?? ??? ? </struts>

六、在WEB-INF下創建pages目錄
?? ?/pages
?? ?在pages目錄下創建頁面
?? ?/pages
?? ??? ?/error.jsp
?? ??? ?/success.jsp
七、創建Action
?? ?1.在src目錄下創建包
?? ??? ?cn.jbit.demoupload.web.action
?? ?2.在包下創建Action
?? ??? ?Action名稱:UploadAction.java
?? ??? ?Action內容:
?? ??? ?public class UploadAction extends ActionSupport {
?? ??? ??? ?private File upload;//文件對象
?? ??? ??? ?private String uploadContentType;//文件類型
?? ??? ??? ?private String uploadFileName;//文件名稱
?? ??? ??? ?public String saveFile(){
?? ??? ??? ??? ?ServletContext servletContext = ServletActionContext.getServletContext();
?? ??? ??? ??? ?String path = servletContext.getRealPath("/upload");
?? ??? ??? ??? ?System.out.println(path+"路徑");
?? ??? ??? ??? ?File newFile = new File(path,uploadFileName);
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?FileUtils.copyFile(upload, newFile);
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return SUCCESS;
?? ??? ??? ?}
?? ??? ??? ?public File getUpload() {
?? ??? ??? ??? ?return upload;
?? ??? ??? ?}
?? ??? ??? ?public void setUpload(File upload) {
?? ??? ??? ??? ?this.upload = upload;
?? ??? ??? ?}
?? ??? ??? ?public String getUploadContentType() {
?? ??? ??? ??? ?return uploadContentType;
?? ??? ??? ?}
?? ??? ??? ?public void setUploadContentType(String uploadContentType) {
?? ??? ??? ??? ?this.uploadContentType = uploadContentType;
?? ??? ??? ?}
?? ??? ??? ?public String getUploadFileName() {
?? ??? ??? ??? ?return uploadFileName;
?? ??? ??? ?}
?? ??? ??? ?public void setUploadFileName(String uploadFileName) {
?? ??? ??? ??? ?this.uploadFileName = uploadFileName;
?? ??? ??? ?}
?? ??? ?}
?? ?3.在struts核心配置文件中添加配置
?? ??? ?<package name="default" namespace="/" extends="struts-default">
?? ? ?? ??? ?<action name="*_uploadAction" class="cn.jbit.demoupload.web.action.UploadAction" method="{1}">
?? ? ?? ??? ??? ?<interceptor-ref name="defaultStack">
?? ? ?? ??? ??? ??? ?<param name="fileUpload.maximumSize">20480</param>
?? ? ?? ??? ??? ??? ?<param name="fileUpload.allowedTypes">image/pjpeg,image/gif</param>
?? ? ?? ??? ??? ??? ?<param name="fileUpload.allowedExtensions">jpg,gif</param>
?? ? ?? ??? ??? ?</interceptor-ref>
?? ? ?? ??? ??? ?<result name="success">/WEB-INF/pages/success.jsp</result>
?? ? ?? ??? ??? ?<result name="input">/WEB-INF/pages/error.jsp</result>
?? ? ?? ??? ?</action>
?? ? ?? ?</package>
八、創建頁面
?? ?頁面名稱:index.jsp
?? ?頁面內容:
?? ?<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
?? ?<%
?? ?String path = request.getContextPath();
?? ?String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
?? ?%>
?? ?
?? ?<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
?? ?<html>
?? ?? <head>
?? ???? <base href="<%=basePath%>">
?? ??? ?
?? ???? <title>My JSP 'index.jsp' starting page</title>
?? ??? ?<meta http-equiv="pragma" content="no-cache">
?? ??? ?<meta http-equiv="cache-control" content="no-cache">
?? ??? ?<meta http-equiv="expires" content="0">?? ?
?? ??? ?<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
?? ??? ?<meta http-equiv="description" content="This is my page">
?? ?? </head>
?? ? ?
?? ?? <body>
?? ???? <form action="${pageContext.request.contextPath }/saveFile_uploadAction.action" method="post" enctype="multipart/form-data">
?? ??? ??? ?上傳文件:
?? ??? ??? ?<input type="file" name="upload"/>
?? ??? ??? ?<input type="submit" value="提交"/>
?? ???? </form>
?? ?? </body>
?? ?</html>

? ??本文轉自 ?素顏豬 ?51CTO博客,原文鏈接:? ??


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/257073.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/257073.shtml
英文地址,請注明出處:http://en.pswp.cn/news/257073.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

將數組作為參數,調用該函數時候給的是數組地址還是整個數組

1、在實際的應用中&#xff0c;數組經常作為函數參數&#xff0c;將數組中的數據傳遞到另外一個函數中&#xff0c;一般來說&#xff0c;傳遞可以采用兩種方法&#xff1a; 1>、數組元素作為函數的實參時&#xff0c;用法跟普通變量作參數相同&#xff0c;將數組元素的值傳遞…

C#項目中常用到的設計模式

C#項目中常用到的設計模式 1. 引言 一個項目的通常都是從Demo開始&#xff0c;不斷為項目添加新的功能以及重構&#xff0c;也許剛開始的時候代碼顯得非常凌亂&#xff0c;毫無設計可言。但是隨著項目的迭代&#xff0c;往往需要將很多相同功能的代碼抽取出來&#xff0c;這也是…

學習筆記(14):Python網絡編程并發編程-文件傳輸功能實現

立即學習:https://edu.csdn.net/course/play/24458/296245?utm_sourceblogtoedu 1.課程目的&#xff1a; 實現客戶端輸入下載文件的命令&#xff0c;然后將命令發送給服務端&#xff0c;服務端再執行下載文件的命令&#xff0c;最后將執行下載文件命令后的結果返回給客戶端&a…

NFS精簡版配置方法

此實驗的前提是防火墻需關閉。 1.關閉iptables /etc/init.d/iptables stop /etc/init.d/iptables status 2.關閉selinux setenforce 0 getenforce Permissive ---出現這個單詞即代表selinux臨時關閉&#xff0c;如需永久關閉則需修改/etc/sysconfig/selinux配置文件 …

Serializable接口中serialVersionUID字段的作用

序列化運行時使用一個稱為 serialVersionUID 的版本號與每個可序列化類相關聯&#xff0c;該序列號在反序列化過程中用于驗證序列化對象的發送者和接收者是否為該對象加載了與序列化兼容的類。 如果接收者加載的該對象的類的 serialVersionUID 與對應的發送者的類的版本號不同&…

重新認知指針

1、把指針指向的變量的數據類型稱為指針的數據類型&#xff1b;而任何一個指針變量本身數據值的類型都是unsigned long int 2.、指針變量名前的符號“*”表示的是指向運算。 3、不要認為“ *p" 是指針變量&#xff0c;指針變量是p而不是*p 4、

分布式數據庫 HBase

原文地址&#xff1a;http://www.oschina.net/p/hbase/ HBase 概念 HBase – Hadoop Database&#xff0c;是一個高可靠性、高性能、面向列、可伸縮的分布式存儲系統&#xff0c;利用HBase技術可在廉價PC Server上搭建起大規模結構化存儲集群。 HBase是Google Bigtable的開源實…

學習筆記(15):Python網絡編程并發編程-進程理論

立即學習:https://edu.csdn.net/course/play/24458/296423?utm_sourceblogtoedu 1.進程&#xff1a;正在運行的一個過程或者一個任務&#xff1b; 2.進程與程序的區別&#xff1a;程序是一堆代碼&#xff0c;程序運行起來就是進程了&#xff0c;一個程序運行兩次&#xff0c;算…

【翻譯】Designing Websites for iPhone X

讓網站適配 iphone X 英文原文地址&#xff1a;https://webkit.org/blog/7929/...本文原文地址&#xff1a;https://github.com/cnsnake11/... The section below about safe area insets was updated on Oct 31, 2017 to reflect changes in the iOS 11.2 beta. 以下關于safe …

指針作為函數參數引用數組的任意元素

void swap(int *a,int*b) {*a*a^*b;*b*a^*b;*a*a^*b; } swap(data[j],data[j1]&#xff09;; int data[10]{13,55,48,13,62,45,754,0,10};以上是我遇到的問題&#xff0c;我覺得調用這個swap函數是不能這樣直接把數組的某個元素直接丟給swap數據 在程序中參加數據處理的量不是指…

使用 Log4Net 記錄日志

第一步&#xff1a;下載Log4Net 下載地址&#xff1a;http://logging.apache.org/log4net/download_log4net.cgi 把下載的 log4net-1.2.11-bin-newkey解壓后&#xff0c;如下圖所示&#xff1a; 雙擊bin文件夾 雙擊net文件夾&#xff0c;選擇針對.NET FramerWork的不同版本 找…

Xcode常用快捷鍵

1. 文件CMD N: 新文件CMD SHIFT N: 新項目CMD O: 打開CMD S: 保存CMDOPtS&#xff1a;保存所有文件CMD SHIFT S: 另存為CMD W: 關閉窗口CMD Q :退出XcodeCMD SHIFT W: 關閉文件2. 編輯CMD [: 左縮進CMD ]: 右縮進CMDshiftF:項目中查找CMDG:查找下一個CMDshiftG:查…

學習筆記(16):Python網絡編程并發編程-開啟子進程的兩種方式

立即學習:https://edu.csdn.net/course/play/24458/296424?utm_sourceblogtoedu #方式一&#xff1a;使用python內置模塊multiprocessing下的process類 from multiprocessing import Process import time#定義進程函數 def task(name):print(%s is running&#xff01;%name)t…

ElasticSearch的API python調用

os json datetime datetime django.http HttpResponse reelasticsearch Elasticsearches Elasticsearch([])res8 es.search({:{:{:{::}}}} ) statistic():():hit res8[][]:a (%hit %hit[])a re.split(a);arow a:id row[] row[]idHttpResponse(a)轉載于:https://blog.51cto…

HDU 1757 A Simple Math Problem (矩陣快速冪)

題目鏈接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1757 在吳神的幫助下才明白如何構造矩陣&#xff0c;還是好弱啊。 此處盜一張圖 1 #include <iostream>2 #include <cstdio>3 #include <cstring>4 #include <cmath>5 #include <al…

Spring學習使用標簽來標記資源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)...

首先&#xff0c;在xml其中新增部分標有下劃線的文件&#xff0c;容器初始化的時候需要掃描包 注意&#xff1a; a. 包款掃描(下劃線部分)一定要加&#xff0c;默認是不掃描整個包。與每一包之間’&#xff0c;’開。如過具有同樣的父包&#xff0c;那么我們能夠用父包來取…

python 判斷字符串時是否是json格式方法

在實際工作中&#xff0c;有時候需要對判斷字符串是否為合法的json格式 解決方法使用json.loads,這樣更加符合‘Pythonic’寫法 代碼示例&#xff1a; Python import json def is_json(myjson):try:json_object json.loads(myjson)except ValueError, e:return Falsereturn Tr…

學習筆記(17):Python網絡編程并發編程-Process對象的其他屬性或方法

立即學習:https://edu.csdn.net/course/play/24458/296427?utm_sourceblogtoedu 1.pid與ppid&#xff1a;pid進程編碼&#xff0c;ppid進程的父進程編碼&#xff1b;os.getpid()查看正在運行的進程編碼&#xff0c;os.getppid()查看正在運行進程的父進程編碼 2.僵尸進程&…

用弦截法求一元三次方程的根x^3-5x^2+16x-80=0 ;帶注釋!

//用弦截法求一元三次方程的根x^3-5x^216x-800 #include<stdio.h>#include<math.h> float f(float x) //定義子函數f(x) x^3-5x^216x-80&#xff0c;當f(x) →0時&#xff0c;則x即為所求的實數根&#xff1b; { float y; y((x-5.0)*x16.0)*x-80.0; …