-
smartupload文件上傳 // 設(shè)置上傳文件保存路徑 String filePath = "E:/Javajava/workspace for j2ee/Study2/WebContent/WEB-INF/file"; File file = new File(filePath); if (!file.exists()) { file.mkdir(); } String result = "上傳成功"; SmartUpload su = new SmartUpload(); try { // 初始化對(duì)象 su.initialize(getServletConfig(), request, response); // 設(shè)置上傳文件大小 su.setMaxFileSize(1024 * 1024 * 10); // 設(shè)置所有文件的大小 su.setTotalMaxFileSize(1024 * 1024 * 100); // 設(shè)置上傳文件的類(lèi)型 su.setAllowedFilesList("txt,jpg,gif"); // 設(shè)置禁止上傳的文件類(lèi)型 su.setDeniedFilesList("rar,jsp,js"); // 上傳文件 su.upload(); int count = su.save(filePath); System.out.println("上傳成功了" + count + "個(gè)文件");查看全部
-
jsp+servlet文件下載 // 設(shè)置文件根路徑 String path = "E:/Javajava/workspace for j2ee/Study2/WebContent/WEB-INF/file"; String filename = request.getParameter("filename"); // 定位下載的文件 File file = new File(path, filename); if (file.exists()) { // 設(shè)置文件類(lèi)型 response.setContentType("application/x-msdownload"); // 設(shè)置頭信息 response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\""); // 讀取文件信息 InputStream inputStream = new FileInputStream(file); ServletOutputStream outputStream = response.getOutputStream(); byte b[] = new byte[1024]; int n; while ((n = inputStream.read(b)) != -1) { outputStream.write(b, 0, n); } // 關(guān)閉流 outputStream.close(); inputStream.close(); } else { request.setAttribute("errorResult", "文件不存在"); request.getRequestDispatcher("jsp/01.jsp").forward(request, response); }查看全部
-
jsp+servlet文件上傳 // 設(shè)置保存文件的路徑 String realPath = "E:/Javajava/workspace for j2ee/Study2/WebContent/WEB-INF/file"; File saveFile = new File(realPath, filename); RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile, "rw"); // 從臨時(shí)文件中讀取內(nèi)容 randomFile.seek(startPosition); while (startPosition < endPosition) { randomAccessFile.write(randomFile.readByte()); startPosition = randomFile.getFilePointer(); } // 關(guān)閉輸入流輸出流,刪除臨時(shí)文件 randomAccessFile.close(); randomFile.close(); tempFile.delete(); request.setAttribute("result", "上傳成功"); request.getRequestDispatcher("jsp/01.jsp").forward(request, response);查看全部
-
jsp+servlet文件上傳 // 重新定位文件指針 randomFile.seek(0); long startPosition = 0; int i = 1; // 獲取文件內(nèi)容的開(kāi)頭位置 while ((n = randomFile.readByte()) != -1 && i <= 4) { if (n == '\n') { // 獲取文件指針當(dāng)前的位置 startPosition = randomFile.getFilePointer(); i++; } } // 獲取文件內(nèi)容結(jié)束位置 randomFile.seek(randomFile.length()); long endPosition = randomFile.getFilePointer(); int j = 1; while (endPosition >= 0 && j <= 2) { endPosition--; randomFile.seek(endPosition); if (randomFile.readByte() == '\n') { j++; } } endPosition--;查看全部
-
jsp+servlet文件上傳 // 獲取文件的名稱(chēng) RandomAccessFile randomFile = new RandomAccessFile(tempFile, "r"); randomFile.readLine(); // Content-Disposition: form-data; name="myfile"; filename="hello.txt" String str = randomFile.readLine(); int beginIndex = str.lastIndexOf("filename") + 10; int endIndex = str.lastIndexOf("\""); String filename = str.substring(beginIndex, endIndex); System.out.println("文件名為:" + filename);查看全部
-
jsp+servlet文件上傳 System.out.println("已接受到請(qǐng)求..."); // 從request中獲取流信息 InputStream fileSource = request.getInputStream(); // 創(chuàng)建一個(gè)臨時(shí)保存的文件,存在tempFile中 String tempFileName = "E:/Javajava/workspace for j2ee/Study2/WebContent/WEB-INF/tempFile/tempFile.txt"; File tempFile = new File(tempFileName); // 創(chuàng)建一個(gè)輸出流指向臨時(shí)文件 FileOutputStream outPutStream = new FileOutputStream(tempFile); byte b[] = new byte[1024]; int n; // 將信息寫(xiě)入到文件中 while ((n = fileSource.read(b)) != -1) { outPutStream.write(b, 0, n); } // 關(guān)閉輸出輸入流 outPutStream.close(); fileSource.close();查看全部
-
接收文件到臨時(shí)文件; 將臨時(shí)文件中內(nèi)容寫(xiě)入到 指定文件; 刪除臨時(shí)文件; 問(wèn)題:可以直接寫(xiě)入到 指定文件中嗎?查看全部
-
獲取文件結(jié)束位置時(shí),為什么不用 getFilePointer()方法,而是 endPosition-- ?查看全部
-
文件下載原理查看全部
-
文件上傳下載原理查看全部
-
文件下載原理查看全部
-
enctype屬性查看全部
-
struts2上傳文件查看全部
-
Struts2的文件上傳下載實(shí)現(xiàn): 1、前臺(tái)form表單method="post" enctype="multipart/form-data",Struts2在原有的上傳解析器繼承上做了進(jìn)一步的封裝,簡(jiǎn)化了文件上傳; 2、Struts2默認(rèn)使用的是Jakarta和Commons-FileUpload的文件上傳框架,使用時(shí)需要導(dǎo)入相關(guān)的jar包查看全部
-
上傳原理查看全部
舉報(bào)
0/150
提交
取消