Web下文件上傳下載的路徑問題


 

工程結構

 

1.生成一個文件到指定文件夾下

//產生一個唯一的名字
this.setFileName(String.valueOf(System.currentTimeMillis()));
String path = ServletActionContext.getServletContext().getRealPath("/template/WordExportTemplate");
//工程下的完整路徑名
String filepath = path +"\\" + fileName + ".doc";




//寫入文件
Writer  out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath,true), "utf-8"), 10240);
out.write("   ");

2.將指定文件下的文件輸出到客戶端(將上面產生的文件輸出)

public InputStream getInputStream() throws Exception {
        create();
        String path = ServletActionContext.getServletContext().getRealPath("/template/WordExportTemplate");
        String filepath = path +"\\" + fileName + ".doc";
        //完整的路徑名
        File file = new File(filepath);
        //只用返回一個輸入流
        return FileUtils.openInputStream(file);
    }

3.將客戶端上傳的文件以特定名字存到指定文件夾下:

public String execute() throws Exception{
        
        ServletContext servletContext = ServletActionContext.getServletContext();
//        fileNameFileName表示文件上傳時候的名字,也可以自己用UUID定義一個新的名字
        String dir = servletContext.getRealPath("/template/ExcelImportTemplate/"+fileNameFileName);
        System.out.println(dir);
//        文件輸出流,寫到dir指定的目錄與名字
        FileOutputStream outputStream = new FileOutputStream(dir);
//        打開上傳的文件的輸入流
        FileInputStream inputStream = new FileInputStream(fileName);
        byte[] buffer = new byte[1024];
        int len = 0;
//        從此輸入流中將最多 b.length 個字節的數據讀入一個 byte 數組中.讀入緩沖區的字節總數,如果因為已經到達文件末尾而沒有更多的數據,則返回 -1。
        while((len = inputStream.read(buffer))!=-1){
//            將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此文件輸出流。
            outputStream.write(buffer, 0, len);
        }
        inputStream.close();
        outputStream.close();
        return SUCCESS;
    }

 

4.讀取指定文件夾下指定名字的文件:

ServletContext servletContext = ServletActionContext.getServletContext();
//        fileNameFileName表示文件上傳時候的名字,也可以自己用UUID定義一個新的名字
String dir = servletContext.getRealPath("/template/ExcelImportTemplate/"+fileNameFileName);
File file = new File(dir);
FileInputStream openInputStream = FileUtils.openInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(openInputStream);

 

5. FileInputStream   FileOutputStream是類。可以直接讀取與寫。

 

構造方法

例如:

public InputStream getInputStream() throws Exception {
        create();
        String path = ServletActionContext.getServletContext().getRealPath("/template/ExcelExportTemplate");
        String filepath = path +"\\" + fileName + ".xls";
        File file = new File(filepath);
        //只用返回一個輸入流
        return FileUtils.openInputStream(file);
    }

 

String path = ServletActionContext.getServletContext().getRealPath("/template");

File tempFile = new File(path, templateName + ".xls");
tempFile.delete();
tempFile.createNewFile();

FileOutputStream stream
= FileUtils.openOutputStream(tempFile);

 

 

1 ServletContext servletContext = ServletActionContext.getServletContext();
2 // fileNameFileName表示文件上傳時候的名字,也可以自己用UUID定義一個新的名字
3  String dir = servletContext.getRealPath("/template/ExcelImportTemplate/"+fileName);
4 FileOutputStream outputStream = new FileOutputStream(dir);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM