📄 uploadconfigurationfileaction.java
字号:
/* * UploadConfigurationFileAction.java * * Created on October 7, 2003, 5:44 PM */package gov.nist.struts.webapp.upload;import gov.nist.security.authentication.UserTag;import java.io.File;import java.io.InputStream;import java.io.IOException;import java.io.OutputStream;import java.io.FileOutputStream;import java.io.FileNotFoundException;import org.apache.struts.upload.FormFile;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 javax.servlet.http.*;import javax.servlet.*;import org.apache.log4j.Logger;/** * This class takes the UploadForm and retrieves the text value * and file attributes and puts them in the request for the display.jsp * page to display them * * @author Jean Deruelle * @version $Revision: 1.4 $ $Date: 2004/04/21 23:32:51 $ */public class UploadConfigurationFileAction extends Action { /** log4j logging*/ static Logger logger = Logger.getLogger(UploadAction.class); /** * @see org.apache.struts.action.Action#perform(org.apache.struts.action.ActionMapping actionMapping, org.apache.struts.action.ActionForm actionForm, javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) */ public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { //Get the application context ServletContext applicationContext=this.getServlet().getServletContext(); //Get the upload Path name in the session context String uploadPath=(String)request.getSession().getAttribute("uploadPath"); //Get the user from the session context UserTag userTag=(UserTag)request.getSession().getAttribute("user"); if (form instanceof UploadForm) { UploadForm theForm = (UploadForm) form; //retrieve the file representation FormFile file = theForm.getTheFile(); //retrieve the file name String fileName= file.getFileName(); //retrieve the content type String contentType = file.getContentType(); //retrieve the file size String size = (file.getFileSize() + " bytes"); //retrieve the fully qualified name of the user's service main class String path=theForm.getPath(); String pathFile=uploadPath+ "dump"+ File.separatorChar+ path+ File.separatorChar+ fileName; logger.info("Writing of the file to the following path : "+pathFile); //Upload the file try { //retrieve the file data InputStream stream = file.getInputStream(); //write the file to the file specified OutputStream bos =null; bos = new FileOutputStream(pathFile); //we upload the file int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); //close the stream stream.close(); } catch (FileNotFoundException fnfe) { logger.debug("FileNotFoundException "+fnfe); return mapping.findForward("error"); } catch (IOException ioe) { logger.debug("IOException "+ ioe); return mapping.findForward("error"); } //File uploaded //destroy the temporary file created file.destroy(); logger.info("Name of file uploaded : "+pathFile); if(userTag.getUserGroup().equals("admin")) return mapping.findForward("admin"); //return a forward to display.jsp return mapping.findForward("monitor"); } //this shouldn't happen return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -