2

I'm doing a simple file upload in jsp. And i seem to have been stopped by this simple path issue. I'm developing on windows but will propably deploy on a linux machine. so i have the tmpdir and the finaldir under mywebsite/tempfiledir and mywebsite/finalfiledir

so i use this code (snippet of the servlet)

public class SyncManagerServlet extends HttpServlet {
 private static final String TMP_DIR_PATH = "/tempfiledir";
 private File tempDir;
 private static final String DESTINATION = "/finalfiledir";
 private File destinationDir;

 public void init(ServletConfig config){
    try {
        super.init(config);

        tempDir = new File(getAbsolute(TMP_DIR_PATH));

        if(!tempDir.isDirectory()){
            throw new ServletException(TMP_DIR_PATH + " is not a Directory");
        }

        destinationDir = new File(getAbsolute(DESTINATION));
        if(!destinationDir.isDirectory()){ 
            throw new ServletException(DESTINATION + " is not a Directory");
        }

    } catch (ServletException ex) {
        Logger.getLogger(OrliteSyncManagerServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
}


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String email = request.getParameter("email");
    String path = request.getContextPath();
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();

    fileItemFactory.setRepository(tempDir);
    ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);


    try {


        List items = uploadHandler.parseRequest(request);
        Iterator itr = items.iterator();
        while(itr.hasNext()){
            FileItem item = (FileItem) itr.next();

            if( item.isFormField() && item != null ){

                out.println("<html>");
                out.println("<head>");
                out.println("<body>");
                out.println("your email: " + item.getString() + " has been submited and context path is "+ request.getContextPath() );
                out.println("</body>");
                out.println("</head>");
                out.println("</html>");

            } else {
                out.println("the uploaded file name is  : " + item.getName());
                out.println("content type  is  : " + item.getContentType());
                out.println("Size  is  : " + item.getSize());
                File file = new File(destinationDir, FilenameUtils.getName(item.getName()));

                item.write(file);
            }

public String getAbsolute(String relativepath){
    return getServletContext().getRealPath(relativepath);
}


//......
}

i'm having this exception

java.io.FileNotFoundException: D:\WORK\java\netbeansProject\Projects\mywebsite-webapp\target\mywebsite\finalfiledir (Access is denied)

i can't figure out why the relative path is failing. I've noticed in a lot of samples online people uses full path for the tempdir. So in my case where i have to worry about linux on deployment, what's the workaround?But first i'ld like to understand why the path i've given is wrong.

Thanks for reading this! so 2 issues here

1 how to solve this path immediate issue?

2 how to do it in a more portable way (with linux privileges in mind)?

thanks!

4

2 に答える 2

1

1このパスの当面の問題を解決する方法は?

先頭のスラッシュで始まるパスは、現在の作業ディレクトリからの相対パスではありません。これらは絶対的なものであり、Windowsでは現在の作業ディスク(サーバーが実行されている場所であり、サーバーの起動方法によって異なります)のみを指します。

あなたの場合、Windowsの場合はWindowsとサーバーがインストールされている"/tempfiledir"ことを指し、Linuxの場合はを指します。フォルダがないことをタイムリーに見つけるために、同様にチェックを行う別のチェックインメソッドを追加する必要があります。C:\tempfiledirC:\/tempfiledirinit()File#exists()

2より移植性の高い方法でそれを行う方法(Linuxの特権を念頭に置いて)?

パス区切り文字のようなWindows固有のディスクラベルC:\を使用/せず、パス区切り文字として使用しない限り、これについて心配する必要はありません。

ファイルをWebコンテンツに書き込むことを本当に主張する場合は、次の2つの点に注意する必要があります。1)WARとしてデプロイする場合、WARがservletcontainerによって拡張された場合にのみ機能します。2)WARを再デプロイすると、すべて(アップロードされたすべてのファイル)が消去されます。

これは、相対Webパスを絶対ディスクファイルシステムパスに変換して、さらに使用したり、組み合わせたりできるようにする方法ですFile

String relativeWebPath = "/tempfiledir";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
File file = new File(absoluteDiskPath, filename);

問題とは関係ありitem.getName()ません。特定のWebブラウザ(具体的には、MSIEファミリ)でクライアント側の完全なパスが返されます。Commons Fileupload FAQFilenameUtils#getName()によると、でファイル名として使用する前に呼び出す必要がありnew File()ます。そうしないと、そこでも大混乱が発生します。

于 2010-11-23T18:22:21.937 に答える
0

一時ファイルを保存するのに最適な場所は、システムプロパティjava.io.tmpdirから取得できるシステム一時ディレクトリだと思います。ユーザーがこのディレクトリに書き込めない環境を見たことがありません。

于 2010-11-23T19:47:03.443 に答える