SSH架構的簡單上傳功能的實現

來源:互聯網
上載者:User

標籤:3.0   form   channel   eal   roo   exists   efault   package   attr   

1.建立項目。

2.匯入開發包。

3.配置web.xml.

配置內容就是配置struct2的內容如下:

<?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">
<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>

<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>

</web-app>

4.編寫上傳檔案的Action類:--首先我再webroot檔案夾下建了一個upload的檔案夾(項目源碼中,上傳的檔案在伺服器的該檔案夾下)

public class FileUpAction {

//定義好下邊的三個欄位屬性,其中類型跟檔案名稱的首碼一樣,file是前台接收的input框的name值跟該欄位名一樣,並產生get、set方法
//File類型,跟前台的name一致
private File abc;
//上傳的檔案類型
private String abcContentType;
//上傳的檔案名稱--原本的檔案名稱
private String abcFileName;

//這是一個檔案上傳的類,可以直接是使用

private void doCopyFile(File srcFile, File destFile) throws IOException {
if (destFile.exists() && destFile.isDirectory()) {
throw new IOException("Destination ‘" + destFile + "‘ exists but is a directory");
}

FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel input = null;
FileChannel output = null;
HttpServletRequest request = ServletActionContext.getRequest();
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
input = fis.getChannel();
output = fos.getChannel();
long size = input.size();
long pos = 0;
long count = 0;
while (pos < size) {
//count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
count = size - pos > 1024*1024*30 ? 1024*1024*30 : size - pos;

long percent = Math.round((double)pos/size*100);
request.getSession().setAttribute("percent", percent);

pos += output.transferFrom(input, pos, count);
}
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(fis);
}

}

//寫一個方法執行檔案上傳

public void upload() throws Exception{

String path = ServletActionContext.getRequest().getRealPath("/upload");

File destFile = new File(new File(path),abcFileName);

doCopyFile(abc, destFile);
}


public File getAbc() {
return abc;
}

public void setAbc(File abc) {
this.abc = abc;
}

public String getAbcContentType() {
return abcContentType;
}

public void setAbcContentType(String abcContentType) {
this.abcContentType = abcContentType;
}

public String getAbcFileName() {
return abcFileName;
}

public void setAbcFileName(String abcFileName) {
this.abcFileName = abcFileName;
}

}

5.配置structs.xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<!-- 該屬性指定上傳的開發模式 -->
<constant name="struts.devMode" value="false" />
<!-- 該屬性指定上傳檔案的臨時儲存路徑 -->
<constant name="struts.multipart.saveDir" value="c:/upload" />
<!--上傳檔案的大小限制 -->
<constant name="struts.multipart.maxSize" value="99999999999" />


<!-- http://127.0.0.1:8080/0613/eg2Action_upload.action -->

<package name="demo01Mg" extends="struts-default">
<!-- 上傳配置 -->
<action name="fileup" class="com.demo.FileUpAction" method="upload">

</action>

</package>


</struts>

 6.前台去提交上傳檔案,我再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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>

<!--enctype="multipart/form-data"一定要有,定義的類型。input的type是filen,name就是FileUpAction中的file屬性的名字,一定要一致-->
<form action="fileup.action" method="post"  enctype="multipart/form-data">
<input type="file" name="abc" /><br /> <input type="submit" id="btn"
value="提交" />
</form>

</body>
</html>

 完成上邊的就可以啟動伺服器去訪問該項目,《《《《《注意提交後的檔案到伺服器的uoload檔案夾中去了,你工作空間的專案檔夾下沒有上傳的檔案

SSH架構的簡單上傳功能的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.