1. 程式人生 > >javaweb實驗---Struts2框架開發應用

javaweb實驗---Struts2框架開發應用

upload.html:

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>簡單的檔案上傳</title> </head> <body> <form action="upload.action" method="post" enctype="multipart/form-data">       檔案標題:<input type="text" name="title" /><br>       選擇檔案:<input type="file" name="upload" /><br>     <input value="上傳" type="submit" /> </form> </body> </html>

上傳成功之後跳轉的頁面succ.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>1</title>          <!--     <link rel="stylesheet" type="text/css" href="styles.css">     -->

  </head>      <body>     <h3>上傳成功!!!</h3>   </body> </html>

Struts.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.custom.i18n.resources" value="globalMessages"/>     <constant name="struts.i18n.encoding" value="GBK"/>

    <package name="Action" extends="struts-default">              <action name="upload" class="Action.UploadAction">             <param name="savePath">/upload</param>             <result>/succ.jsp</result>             </action>             </package> </struts>

UploadAction.java:

package Action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {          private String title;     private File upload;     private String uploadContentType;     private String uploadFileName;     private String savePath;     public String getTitle() {         return (this.title);     }     public void setTitle(String title) {         this.title = title;     }     public File getUpload() {         return (this.upload);     }     public void setUpload(File upload) {         this.upload = upload;     }     public String getUploadContentType() {         return (this.uploadContentType);     }     public void setUploadContentType(String uploadContentType) {         this.uploadContentType = uploadContentType;     }     public String getUploadFileName() {         return (this.uploadFileName);     }     public void setUploadFileName(String uploadFileName) {         this.uploadFileName = uploadFileName;     }     public String getSavePath()throws Exception {         return ServletActionContext.getRequest().getRealPath(savePath);              }     public void setSavePath(String savePath) {         this.savePath = savePath;     }          @Override     public String execute()throws Exception{         System.out.println("開始上傳單個檔案-------------");         System.out.println(getSavePath());         System.out.println("============"+getUploadFileName());         System.out.println("============"+getUploadContentType());         System.out.println("============"+getUpload());         //以伺服器的檔案儲存地址和原檔名建立上傳檔案傳輸流         FileOutputStream fos = new FileOutputStream(getSavePath() +"\\" +         getUploadFileName());         FileInputStream fis = new FileInputStream(getUpload());         byte[] buffer = new byte[1024];         int len = 0;         while((len = fis.read(buffer))>0){             fos.write(buffer,0,len);         }         return SUCCESS;     }      }

測試截圖:

上傳成功後跳轉到succ.jsp頁面,並且顯示上傳成功: