最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

action訪問不了的原因,STRUTS2附件上傳和下載,PPT,struts.xml,視頻筆記【詩(shī)書畫唱】

2021-01-07 23:49 作者:詩(shī)書畫唱  | 我要投稿

個(gè)人的總結(jié)&本期導(dǎo)讀

enctype="multipart/form-data",文件上傳和下載的PPT,注意,視頻筆記,附件上傳不是上傳到本地,eclipse,而是上傳到tomcat服務(wù)器上。每次啟動(dòng)tomcat服務(wù)器的時(shí)候都是會(huì)把項(xiàng)目都發(fā)送到tomcat服務(wù)器中。當(dāng)你的項(xiàng)目上線的時(shí)候,是可以脫離eclipse的。

。修改附件上傳的文件名為時(shí)間戳等的方法的關(guān)鍵之處,附件上傳的服務(wù)器的訪問路徑中需要注意的內(nèi)容。附件上傳圖片。個(gè)人的理解:附件上傳時(shí)在QQ瀏覽器等中顯示才效果好,在eclipse中顯示會(huì)出問題,可能是eclipse中的顯示有些太低配了。






(如果struts.xml的內(nèi)容不對(duì),action是會(huì)訪問不了的,會(huì)報(bào)404。如果直接復(fù)制項(xiàng)目?jī)?nèi)容到另一個(gè)同名項(xiàng)目就會(huì)struts.xml,要把struts.xml復(fù)制到src下,覆蓋原來的struts.xml。)

附件上傳圖片 START





附件上傳圖片?END




文件上傳和下載的PPT START

創(chuàng)建表單頁(yè)面,注意必須有enctype="multipart/form-data"配置。文件上傳控件的name屬性必須存在。

創(chuàng)建action類,假設(shè)表單中的type控件的name為upload,則在action中添加File類型的upload屬性和String類型的uploadFileName屬性以及他們的getter和setter。

注意:紅色部分必須一致,即若type控件的name屬性變?yōu)閍bc,則后面的兩個(gè)屬性名也必須變?yōu)閍bc和abcFileName。



請(qǐng)實(shí)現(xiàn)功能:

1、當(dāng)上傳的圖片成功時(shí),跳轉(zhuǎn)到result.jsp頁(yè)面后能夠馬上在頁(yè)面中展示這個(gè)圖片。

2、提供一個(gè)下載頁(yè)面,展示可以下載的文件列表,當(dāng)點(diǎn)擊文件名時(shí)可以直接下載到該文件。




文件上傳和下載的PPT END



講義 START

在STRUTS2框架中實(shí)現(xiàn)附件上傳


將你的電腦上的文件發(fā)送到服務(wù)器tomcat上的過程,是客戶端電腦和服務(wù)器之間的文件傳輸


將eclipse中的class文件以及樣式html文件發(fā)布到tomcat服務(wù)器上,然后啟動(dòng)tomcat服務(wù)器,才可以訪問項(xiàng)目中的頁(yè)面和程序


http://localhost:8888/J190802/img/2.png





講義 END

例子 START

package com.jy.action;


import java.io.InputStream;


import org.apache.struts2.ServletActionContext;


public class DownloadAction {

private String fileName;//需要下載的文件名

public String getFileName() {

return fileName;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

//返回InputStream類型的方法名必須以get開頭,

//get后面的第一個(gè)字母必須大寫

public InputStream getDlFile(){

//將需要下載的文件轉(zhuǎn)換成了流

InputStream ins = ServletActionContext.getServletContext()

.getResourceAsStream(fileName);

return ins;

}

? ? public String download(){

? ? return "success";

? ? }

}

package com.jy.action;


import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;


import org.apache.struts2.ServletActionContext;


public class UploadAction {

//File類型的變量名和type="file"的控件的name屬性保持一致

private File up;//選中的需要上傳的文件對(duì)象

//type="file"的控件的name屬性+FileName

private String upFileName;//選中的需要上傳的文件名

private String imgPath;//上傳的圖片在服務(wù)器上的url路徑

? ? public File getUp() {

return up;

}

public void setUp(File up) {

this.up = up;

}

public String getUpFileName() {

return upFileName;

}

public void setUpFileName(String upFileName) {

this.upFileName = upFileName;

}

public String getImgPath() {

return imgPath;

}

public void setImgPath(String imgPath) {

this.imgPath = imgPath;

}

public String upload(){

? ? //獲取服務(wù)器的路徑

String realPath = ServletActionContext

.getServletContext().getRealPath("");

//項(xiàng)目所在服務(wù)器的路徑

//E:\JAVA\apache-tomcat-7.0.56\webapps\J190802

System.out.println(realPath);

//設(shè)置將上傳的文件保存在服務(wù)器的哪個(gè)目錄下

String fullPath = realPath + "/img";

//E:\JAVA\apache-tomcat-7.0.56\webapps\J190802/img

System.out.println("將選中的文件上傳到的位置是:" + fullPath);


//上傳圖片在服務(wù)器上的訪問路徑

imgPath = "http://localhost:8080/J190802/img/" + upFileName;

//檢查上面的文件夾是否存在

File file = new File(fullPath);

if(! file.exists()) {//如果這個(gè)文件夾不存在,我就創(chuàng)建它

file.mkdir();

}

FileInputStream fis = null;

FileOutputStream fos = null;

try {

//將up文件拷貝到file文件夾下

//將選中的文件轉(zhuǎn)換成Input流

fis = new FileInputStream(up);

//設(shè)置文件上傳的路徑

fos = new FileOutputStream(fullPath + "/" + upFileName);

//進(jìn)行拷貝

byte[]buffer = new byte[1024];

int len = 0;

while((len = fis.read(buffer)) > 0) {

fos.write(buffer);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

fos.close();

fis.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

? ? return "success";

? ? }

}


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

? ? ?<package name="file" namespace="/fl" extends="struts-default">

? ? ? ? ?<action name="uploadAc" class="com.jy.action.UploadAction"

? ? ? ? ? ? ?method="upload">

? ? ? ? ? ? ?<result>/result.jsp</result>

? ? ? ? ?</action>

? ? ? ? ?<action name="downloadAc" class="com.jy.action.DownloadAction"

? ? ? ? ? ? ?method="download">

? ? ? ? ? ? ?<result type="stream">

? ? ? ? ? ? ? ? ?<param name="contentType">text/plain</param>

? ? ? ? ? ? ? ? ?<param name="contentDisposition">

? ? ? ? ? ? ? ? ? ? ?<!-- fileName就是DownloadAction的一個(gè)屬性,表示需要下載的文件名 -->

? ? ? ? ? ? ? ? ? ? ?attachment;fileName="${fileName}"

? ? ? ? ? ? ? ? ?</param>

? ? ? ? ? ? ? ? ?<!-- 找到action中的返回值為InputStream的方法,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 將get去掉,將首字母改成小寫填入到inputName中 -->

? ? ? ? ? ? ? ? ?<param name="inputName">dlFile</param>

? ? ? ? ? ? ? ? ?<param name="bufferSize">1024</param>

? ? ? ? ? ? ?</result>

? ? ? ? ?</action>

? ? ?</package>

</struts>


<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

? <display-name>J190802</display-name>

? <!-- struts2框架的配置 -->

? <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.html</welcome-file>

? ? <welcome-file>index.htm</welcome-file>

? ? <welcome-file>index.jsp</welcome-file>

? ? <welcome-file>default.html</welcome-file>

? ? <welcome-file>default.htm</welcome-file>

? ? <welcome-file>default.jsp</welcome-file>

? </welcome-file-list>

</web-app>

<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=basePath%>">

? ? ? ? <title>附件下載</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">

? ? ? ? <style type="text/css">

? ? ? ? ? ? *{

? ? ? ? ? ? ? ? font-size: 50px;

? ? ? ? ? ? }

? ? ? ? </style>

? ? ? ? <script type="text/javascript">

? ? ? ? ? ? //文件名不要寫中文

? ? ? ? ? ? function downloadFile(fileName){

? ? ? ? ? ? fileName = 'doc/' + fileName;

? ? ? ? ? ? location.hreff = 'fl/downloadAc.action?fileName=' + fileName;

? ? ? ? ? ? }

? ? ? ? </script>

? ? </head>

? ? <body>

? ? ? ? <a hreff="javascript:downloadFile('g190802.xlsx');">下載</a>

? ? </body>

</html>


<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=basePath%>">

? ? ? ? <title></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>

? ? ? ? <h1>附件上傳成功</h1>

? ? ? ? <img srcc="${imgPath }" />

? ? </body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=basePath%>">

? ? ? ? <title>附件上傳</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">

? ? ? ? <style type="text/css">

? ? ? ? ? ? *{

? ? ? ? ? ? ? ? font-size: 50px;

? ? ? ? ? ? }

? ? ? ? </style>

? ? </head>

? ? <body>

? ? ? ? <form action="fl/uploadAc.action" method="post" enctype="multipart/form-data">

? ? ? ? ? ? <label>賬號(hào):</label><input type="text" name="act" />

? ? ? ? ? ? <br>

? ? ? ? ? ? <input type="file" name="up" />

? ? ? ? ? ? <input type="submit" value="上傳" />

? ? ? ? </form>

? ? </body>

</html>



個(gè)人的理解:可能是eclipse中的顯示有些太低配了。



例子?END

視頻筆記 START


附件上傳不是上傳到本地,eclipse,而是上傳到tomcat服務(wù)器上。


每次啟動(dòng)tomcat服務(wù)器的時(shí)候都是會(huì)把項(xiàng)目都發(fā)送到tomcat服務(wù)器中。




修改附件上傳的文件名為時(shí)間戳等的方法的關(guān)鍵之處 START



修改附件上傳的文件名為時(shí)間戳等的方法的關(guān)鍵之處?END



附件上傳的服務(wù)器的訪問路徑中需要注意的內(nèi)容 START

附件上傳的服務(wù)器的訪問路徑中需要注意的內(nèi)容 END



視頻筆記 END



action訪問不了的原因,STRUTS2附件上傳和下載,PPT,struts.xml,視頻筆記【詩(shī)書畫唱】的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
通河县| 集安市| 南雄市| 瑞丽市| 蓬安县| 茂名市| 平邑县| 滨海县| 长兴县| 九台市| 湘潭市| 奉节县| 子洲县| 林芝县| 安仁县| 永定县| 靖安县| 青州市| 湛江市| 无极县| 丰镇市| 延安市| 沙湾县| 兴安县| 山丹县| 年辖:市辖区| 贡觉县| 苍南县| 南漳县| 中山市| 克山县| 桓台县| 甘谷县| 马山县| 乌审旗| 治多县| 海阳市| 个旧市| 河津市| 崇阳县| 来凤县|