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

📄 uploadaction.java

📁 j2ee程序
💻 JAVA
字号:
/*
 * UploadAction.java
 *
 * Created on 2006年11月29日, 下午2:40
 */

package myPackage;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.upload.FormFile;
/**
 *
 * @author promenade
 * @version
 */

public class UploadAction extends Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        String encoding = request.getCharacterEncoding();
        if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
            response.setContentType("text/html; charset=gb2312");
        }
        UploadActionForm theForm = (UploadActionForm) form;
        FormFile file = theForm.getTheFile();
        FormFile file2=theForm.getTheFile2();
        
        try {
                /*
                 * 取当前系统路径D:\Tomcat5\webapps\coka\ 其中coka 为当前context
                 */
            //String filePath = this.getServlet().getServletContext().getRealPath("/");
            String filePath=this.getServlet().getServletContext().getRealPath("myUploadFiles");
            
            InputStream stream = file.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            OutputStream bos = new FileOutputStream(filePath +"\\online.img");

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            stream.close();
            // save the path
            session.setAttribute("fileName1",(file.getFileName()));
            
            InputStream stream2 = file2.getInputStream();
            ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
            OutputStream bos2 =  new FileOutputStream(filePath +"\\offline.img");
            int bytesRead2 = 0;
            byte[] buffer2 = new byte[8192];
            int i=0;
            while ((bytesRead2 = stream2.read(buffer2, 0, 8192)) != -1) {
                bos2.write(buffer2, 0, bytesRead2);
            }
            bos2.close();
            stream2.close();
                        // save the path
            session.setAttribute("fileName2",(file2.getFileName()));
           
        } catch (Exception e) {
            System.err.print(e);
        }
        
        response.sendRedirect("/dynImage/showImage.jsp");
        return mapping.findForward(SUCCESS);
    }
}

⌨️ 快捷键说明

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