📄 uploadaction.java
字号:
package gov.nist.struts.webapp.upload;import gov.nist.security.authentication.UserTag;import gov.nist.security.bcs.Service;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 java.util.jar.*;import java.util.*;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 org.apache.struts.action.ActionError;import org.apache.struts.action.ActionErrors;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.6 $ $Date: 2004/04/30 19:13:22 $ */public class UploadAction extends Action { /** log4j logging*/ static Logger logger = Logger.getLogger(UploadAction.class); /** * @see org.apache.struts.action.Action#perform(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response) */ 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 user = (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 mainUserClass = theForm.getPath(); //place the data into the request for retrieval from monitor.jsp request.setAttribute("fileName", fileName); //If the user doesn't upload a jar file //we have to create the directory tree for the packages //to well load the class after with the class Loader. String packagePath = null; if (fileName.indexOf(".jar") == -1) { packagePath = createPackagesDirectory(mainUserClass, uploadPath); logger.info( "Writing of the file to the following path : " + packagePath); } else logger.info( "Writing of the file to the following path : " + uploadPath); //Upload the file try { //retrieve the file data InputStream stream = file.getInputStream(); //write the file to the file specified OutputStream bos = null; //If the main class doesn't have any package name //or if the user upload a jar, it is uploaded in a tmp path //so that if a user previously uploaded a jar, it is removed before if (packagePath == null) bos = new FileOutputStream( uploadPath + File.separatorChar +"tmp"+File.separatorChar + fileName); //else we upload it to the package path //(which is the upload path + the packages name directory) else bos = new FileOutputStream( packagePath + File.separatorChar + fileName); //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 logger.info("Name of file uploaded : " + fileName); if (mainUserClass != null) logger.info( "Fully qualified name of main class : " + mainUserClass); //if the user doesn't enter the name of a main class to execute on the website if (mainUserClass == null || mainUserClass.length() < 1) { //and the file is a jar if (fileName.indexOf(".jar") != -1) { try { //we get the Main-Class attribute of the manifest JarFile jarFile = new JarFile( uploadPath + File.separatorChar +"tmp"+File.separatorChar + fileName); mainUserClass = getMainClassFromManifest(jarFile); request.setAttribute("MainClass", mainUserClass); //if there is not, we didn't launch the service if (mainUserClass == null) { ActionErrors errors = new ActionErrors(); errors.add( "path", new ActionError("error.fullClassName.missing")); saveErrors(request, errors); return mapping.findForward("error"); } } catch (IOException ioe) { ioe.printStackTrace(); return mapping.findForward("error"); } } else return mapping.findForward("error"); } else { request.setAttribute("MainClass", mainUserClass); } request.setAttribute("contentType", contentType); request.setAttribute("size", size); //If the guy tries to re-upload his service without stopping it before //it is stopped and removed for him Hashtable monitors = (Hashtable) applicationContext.getAttribute("monitors"); //get the service from the monitors Service service=(Service)monitors.get(mainUserClass+":"+user.getUri()); if(service!=null){ Process process=service.getProcess(); if(process!=null){ process.destroy(); service=null; monitors.remove(mainUserClass+":"+user.getUri()); } } //Remove physically the files of the previous service if any removeAllFilesFromAService(fileName, mainUserClass, uploadPath); if (packagePath == null){ try { //retrieve the file data InputStream stream = file.getInputStream(); //write the file to the file specified OutputStream bos = null; //If the main class doesn't have any package name //or if the user upload a jar, it is uploaded in a tmp path //so that if a user previously uploaded a jar, it is removed before if (packagePath == null) bos = new FileOutputStream( uploadPath +File.separatorChar + fileName); //else we upload it to the package path //(which is the upload path + the packages name directory) else bos = new FileOutputStream( packagePath + File.separatorChar + fileName); //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"); } //destroy the temporary file created File tmpFile=new File(uploadPath + File.separatorChar +"tmp"+File.separatorChar + fileName); if(tmpFile.exists()){ tmpFile.delete(); } } //put the service newly uploaded monitors.put( mainUserClass + ":" + user.getUri(), new Service(mainUserClass,fileName,true,theForm.getServiceType())); //Creates the mapping between the username, password and the service of the user //in the file specified in the configuration.xml file under the tag 'mapping' writeNotifyInXMLMappingFile( (String) applicationContext.getAttribute("mappingFile"), user, fileName, mainUserClass, theForm.getServiceType()); //destroy the temporary file created file.destroy(); //return a forward to display.jsp return mapping.findForward("display"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -