⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadaction.java

📁 学习struts标签库,里面随带了相关的演示程序代码,非常好学.
💻 JAVA
字号:
package org.apache.struts.webapp.exercise;


import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;


public class UploadAction extends Action {


    /**
     * Forward to the input form if "Save" was pressed or the main menu
     * if "Cancel" was pressed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param actionForm The optional ActionForm bean for this request
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     *
     * @exception Exception if business logic throws an exception
     */
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {

    	String store_path = servlet.getServletContext().getRealPath("/fileupload");
    	System.out.println(store_path);
    	UploadBean filebean = (UploadBean) form;
    	
    	FormFile file = filebean.getFile();
    	if (file == null){
    		return mapping.findForward("input");
    	}
    	
    	String filename = file.getFileName();
    	filebean.setFilename(filename);
    	String size = Integer.toString(file.getFileSize()) + "bytes";
    	filebean.setSize(size);
    	
    	InputStream is = file.getInputStream();
    	OutputStream os = new FileOutputStream(store_path + "/" + filename);
    	
    	int bytes = 0;
    	byte [] buffer = new byte[8192];
    	while ((bytes = is.read(buffer,0,8192))!=-1){
    		os.write(buffer,0,bytes);
    	}
    	
    	os.close();
    	is.close();
    	file.destroy();
    	
    	return mapping.findForward("input");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -