如何在jboss中更改上传文件的默认文件夹

时间:2013-12-05 02:55:03

标签: jboss

我正在尝试上传文件,我正在尝试更改上传文件的默认位置。如何改变这个请建议?

package Controller;

import java.io.File;  
import java.io.IOException;  
import java.io.PrintWriter;  
import java.util.Iterator;  
import java.util.List;  

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

import requests.Connect;
import display.DisplayLog;
/** * Servlet implementation class ControlServlet
 */
public class ControlServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;
    boolean result;
    private boolean isMultipart;
    private String filePath;
    private int maxFileSize = 1000 * 1024;
    private int maxMemSize = 1000 * 1024;
    private File file ;

    public ControlServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**     
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        HttpSession session=request.getSession();
        String userName = request.getParameter("username");
      isMultipart = ServletFileUpload.isMultipartContent(request);
        response.setContentType("audio/mpeg3;audio/x-mpeg-3;video/mpeg;video/x-mpeg;text/xml");
        PrintWriter out = response.getWriter( );

        if (isMultipart) {  
            // Create a factory for disk-based file items  
            FileItemFactory factory = new DiskFileItemFactory();  
            // Create a new file upload handler  
            ServletFileUpload upload = new ServletFileUpload(factory);  
           try {  
              // Parse the request  
              List items = upload.parseRequest(request);  
             Iterator iterator = items.iterator();  
             while (iterator.hasNext()) {  
               FileItem item = (FileItem) iterator.next();  
               if (!item.isFormField())  
                {  
                 String fileName = item.getName();      
                 if (fileName != null) {
                     fileName = FilenameUtils.getName(fileName);
                 }
                 String root = getServletContext().getRealPath("/");  
                 root = "F/images";
                 File path = new File(root + "/uploads");

                 if (!path.exists())  
                  {  
                   boolean status = path.mkdirs();  
                 }  
                 File uploadedFile = new File(path + "/" + fileName);  
                 System.out.println(" Prashant File Upload Location is "); 
               //  System.out.println(uploadedFile.getAbsolutePath());  
                 System.out.println("fileName is " +fileName);
                 System.out.println("root is " + root);
                 System.out.println("path is " + path);
                  if(fileName!="")  
                  {   
                      item.write(uploadedFile);  
                      System.out.println(" Prashant File Upload Location 2 is "); 
                      System.out.println(uploadedFile.getAbsolutePath());  
                      out.println("<h1>File Uploaded Successfully....:-)</h1>");
                  }
                  else
                  {       
                     out.println(uploadedFile.getAbsolutePath());  
                      out.println("<h1>File Uploaded Successfully....:-)</h1>");
                      System.out.println("file not found");  
                  }

               }  
                else  
                {  
                  String abc = item.getString();  
                }  
             }  
           } catch (FileUploadException e) {  
                 out.println(e);  
           } catch (Exception e) {  
                 out.println(e);  
           }  
         }  
          else  
          {  
            out.println("Not Multipart");  
          }          
        System.out.println("print this Prashant" + userName);
        session.setAttribute("username",userName);
        request.setAttribute("username","prashant");
    //  RequestDispatcher myDispatch = request.getRequestDispatcher("Index.jsp");
        //myDispatch.forward(request, response);
}

}

我将此作为默认文件夹F:\ jboss-4.2.3.GA-jdk6 \ jboss-4.2.3.GA \ bin \请帮助我是这个新手

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

 String root = getServletContext().getRealPath("/");

您正在设置容器路径的上传路径,而不是默认路径,而是服务器的起始位置。您可以将上传路径设置为您想要的任何内容,具体取决于您的需求和配置。 您可以使用您选择的目录创建系统属性,您可以将其设置为JBoss配置文件中的动态属性(不确定JBoss4的内容)。