程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 關於在Struts2框架下實現文件的上傳功能,struts2框架

關於在Struts2框架下實現文件的上傳功能,struts2框架

編輯:JAVA綜合教程

關於在Struts2框架下實現文件的上傳功能,struts2框架


struts2的配置過程

(1)在項目中加入jar包

 

(2)web.xml中filter(過濾器)的配置

 

<?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">
  <display-name></display-name>	
    <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>

 

(3)struts.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">
  <display-name></display-name>	
    <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類的編寫

package com.xmgc.sc.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import org.apache.struts2.ServletActionContext;

public class MyUpLoadAction {

	private String title;
	private File upload;//與form表單中的file文件類型的名字是一樣的
	private String uploadContentType;//前綴必須要是上面的upload
	private String uploadFileName;
	

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

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

/*	public String getSavePath() {
		//ServletContext cxt=ServletActionContext.getServletContext();
		//String path=cxt.getRealPath("/");
		//這個獲取的path為:http://localhost:8080/sc      
		//上傳以後變為E:\software\apache-tomcat-6.0.45\webapps\sc
		
		
		return savePath;
		
	}

	public void setSavePath(String savePath) {
		//E:\software\apache-tomcat-6.0.45\webapps\sc/myupload
		this.savePath = ServletActionContext.getServletContext().getRealPath("/myupload");
	}*/
	
	
	public String execute() throws IOException{
		
		System.out.println(title);//標題
		System.out.println(uploadContentType);//准備上傳的文件的文件類型
		System.out.println(uploadFileName);//准備上傳的文件的文件名,記住還有擴展名
		System.out.println(upload);
		
		String realPath=ServletActionContext.getServletContext().getRealPath("/");
		
		String path=realPath+"myupload/"+uploadFileName;
		
		System.out.println(realPath);
		System.out.println(path);
		
		FileInputStream fis=new FileInputStream(upload);
		FileOutputStream fos=new FileOutputStream(path);
		
		byte[] bytes=new byte[1024];//定義一個1024大小的字節數組
		int len=-1;//用來做標志位
		
		
		while((len=fis.read(bytes))>0){
			fos.write(bytes, 0, len);
		}
		
		return null;
	}
}

 

(5)JSP頁面的編寫

<%@ page contentType="text/html;charset=utf-8"%>

<!-- enctype="multipart/form-data",這個是最重要的配置 -->
<form action="myupload.action" enctype="multipart/form-data" method="post">
      文件名:<input type="text" name="title"/><br/>
         上傳:<input type="file" name="upload"/><br/>
        <input type="submit" value="上傳"/> 
</form>

  

經過這次總結,感覺struts2框架下的單文件的上傳還是非常簡單的,只要獲取要存儲在什麼地方的地址,再通過輸入輸出流,寫到這個地址中去就行了。絕大部分工作,struts2已經幫我們做好了

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved