javaweb实验---Struts2框架开发应用

 

upload.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+"/";
%>



 
   
    
    1
    
    

 
  
 
   

上传成功!!!


 

 

Struts.xml:

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    

    
    

    
    
        
            /upload
            /succ.jsp    
        
        
    

 

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

测试截图:

javaweb实验---Struts2框架开发应用_第1张图片

 

上传成功后跳转到succ.jsp页面,并且显示上传成功:

javaweb实验---Struts2框架开发应用_第2张图片

 

你可能感兴趣的:(javaweb,Struts框架)