国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

html5-Java using commons-fileupload to upload files has no effect!
ringa_lee
ringa_lee 2017-06-14 10:51:29
0
2
1067

.

package yyyy.www;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadHandleServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.println("post");
        //得到上傳文件的保存目錄,將上傳的文件存放于WEB-INF目錄下,不允許外界直接訪問,保證上傳文件的安全
        String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
        
        System.out.println(savePath);
        File file = new File(savePath);
        //判斷上傳文件的保存目錄是否存在
        if (!file.exists() && !file.isDirectory()) {
            System.out.println("222");
            System.out.println(savePath+"目錄不存在,需要創(chuàng)建");
            //創(chuàng)建目錄
            file.mkdir();
        }
        //消息提示
        String message = "";
        try{
            //使用Apache文件上傳組件處理文件上傳步驟:
            //1、創(chuàng)建一個DiskFileItemFactory工廠
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2、創(chuàng)建一個文件上傳解析器
            ServletFileUpload upload = new ServletFileUpload(factory);
             //解決上傳文件名的中文亂碼
            upload.setHeaderEncoding("UTF-8"); 
            //3、判斷提交上來的數(shù)據(jù)是否是上傳表單的數(shù)據(jù)
            if(!ServletFileUpload.isMultipartContent(request)){
                //按照傳統(tǒng)方式獲取數(shù)據(jù)
                return;
            }
            //4、使用ServletFileUpload解析器解析上傳數(shù)據(jù),解析結(jié)果返回的是一個List<FileItem>集合,每一個FileItem對應(yīng)一個Form表單的輸入項
            List<FileItem> list = upload.parseRequest(request);
            for(FileItem item : list){
                //如果fileitem中封裝的是普通輸入項的數(shù)據(jù)
                if(item.isFormField()){
                    String name = item.getFieldName();
                    //解決普通輸入項的數(shù)據(jù)的中文亂碼問題
                    String value = item.getString("UTF-8");
                    //value = new String(value.getBytes("iso8859-1"),"UTF-8");
                    System.out.println(name + "=" + value);
                }else{//如果fileitem中封裝的是上傳文件
                    //得到上傳的文件名稱,
                    String filename = item.getName();
                    System.out.println(filename);
                    if(filename==null || filename.trim().equals("")){
                        continue;
                    }
                    //注意:不同的瀏覽器提交的文件名是不一樣的,有些瀏覽器提交上來的文件名是帶有路徑的,如:  c:\a\b.txt,而有些只是單純的文件名,如:1.txt
                    //處理獲取到的上傳文件的文件名的路徑部分,只保留文件名部分
                    filename = filename.substring(filename.lastIndexOf("\")+1);
                    //獲取item中的上傳文件的輸入流
                    InputStream in = item.getInputStream();
                    //創(chuàng)建一個文件輸出流
                    FileOutputStream out = new FileOutputStream(savePath + "\" + filename);
                    //創(chuàng)建一個緩沖區(qū)
                    byte buffer[] = new byte[1024];
                    //判斷輸入流中的數(shù)據(jù)是否已經(jīng)讀完的標(biāo)識
                    int len = 0;
                    //循環(huán)將輸入流讀入到緩沖區(qū)當(dāng)中,(len=in.read(buffer))>0就表示in里面還有數(shù)據(jù)
                    while((len=in.read(buffer))>0){
                        //使用FileOutputStream輸出流將緩沖區(qū)的數(shù)據(jù)寫入到指定的目錄(savePath + "\" + filename)當(dāng)中
                        out.write(buffer, 0, len);
                    }
                    //關(guān)閉輸入流
                    in.close();
                    //關(guān)閉輸出流
                    out.close();
                    //刪除處理文件上傳時生成的臨時文件
                    item.delete();
                    message = "文件上傳成功!";
                }
            }
        }catch (Exception e) {
            message= "文件上傳失?。?;
            e.printStackTrace();
            
        }
        request.setAttribute("message",message);
        request.getRequestDispatcher("/message.jsp").forward(request, response);
    }
}

Judging from the code, the upload folder should be automatically created for me in web-INF.
The page prompts that the upload is successful. But the file is not uploaded.

ringa_lee
ringa_lee

ringa_lee

reply all(2)
曾經(jīng)蠟筆沒有小新

The WEB-INF seen in eclipse is the directory of the project. The place where eclipse is actually released is where your application runs. You can print the absolute path of WEB-INF in the program. If I guessed correctly, it should be in Somewhere under workspace/.metadata/!

Ty80

Go to tomcat’s webapp directory and find the web-inf directory under the folder with the same name as your program. What is actually executed is your compiled class bytecode, so the actual directory address is not the directory of your current code, but the directory where the compiled class bytecode is located, which is tomcat/webapp/{projectName} Under the folder

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template