『壹』 通過java實現文件下載
在jsp/servlet中斷點/多線程下載文件
<%@ page import="java.io.File" %><%@ page import="java.io.IOException" %><%@ page import="java.io.OutputStream" %><%@ page import="java.io.RandomAccessFile" %><%! public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, "r"); java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD()); response.setHeader("Server", "www.trydone.com"); response.setHeader("Accept-Ranges", "bytes"); long pos = 0; long len; len = raf.length(); if (request.getHeader("Range") != null) { response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); pos = Long.parseLong(request.getHeader("Range") .replaceAll("bytes=", "") .replaceAll("-", "") ); } response.setHeader("Content-Length", Long.toString(len - pos)); if (pos != 0) { response.setHeader("Content-Range", new StringBuffer() .append("bytes ") .append(pos) .append("-") .append(Long.toString(len - 1)) .append("/") .append(len) .toString() ); } response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", new StringBuffer() .append("attachment;filename=\"") .append(file.getName()) .append("\"").toString()); raf.seek(pos); byte[] b = new byte[2048]; int i; OutputStream outs = response.getOutputStream(); while ((i = raf.read(b)) != -1) { outs.write(b, 0, i); } raf.close(); fis.close(); }%><% String filePath = request.getParameter("file"); filePath = application.getRealPath(filePath); File file = new File(filePath); downloadFile(request, response, file);%>
是否可以解決您的問題?
『貳』 java io重要嗎
io怎麼不重要?
如果你真的打算做程序員,在以後的工作中,你編程肯定涉及到IO操作。
簡單點,你開發一個網站,如果提供用戶上傳下載文件的,有IO操作吧。
其次,與資料庫打交道,說白了也是IO來的。只是封裝好了
『叄』 java io 的上傳下載 要詳細代碼 有注釋
下載文件
下載文件指的是把伺服器端的文件發送到客戶端,Servlet可以向客戶端發送任意格式的文件。
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
OutputStream out;//輸出響應正文的輸出流
InputStream in;//讀取本地文件的輸入流
String filename = req.getParameter("filename");
if(filename == null) {
out = resp.getOutputStream();
out.write("Please input filename".getBytes());
out.close();
return;
}
//獲得本地輸入流
in = getServletContext().getResourceAsStream("/store/" + filename);
int length = in.available();
//設置響應正文的MIME類型
resp.setContentType("application/force-download");
resp.setHeader("Content-length", String.valueOf(length));
resp.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\" ");
//把本地文件發送給客戶端
out = resp.getOutputStream();
int byteRead = 0;
byte[] buffer = new byte[512];
while((byteRead = in.read(buffer)) != -1) {
out.write(buffer, 0, byteRead);
}
in.close();
out.close();
}
文件上傳
上傳文件是要把客戶端文件上傳到伺服器。當客戶端向伺服器端上傳文件時,客戶端發送的HTTP請求正文採用"multipart/form-data"數據類型。
不管HTTP請求正文為何種數據類型,Servlet容器都會把HTTP請求包裝成一個HttpServletRequest對象。當請求正文為"multipart/form-data"數據類型時,Servlet直接從HttpServletRequest對象解析出復合表單的每個子部分仍然是一個非常復雜的工作。
為了簡化對"multipart/form-data"數據的處理過程,可以利用Apache開源軟體組織提供的兩個軟體包來實現上傳。
Fileupload軟體包commons-fileupload-1.2.1.jar
Io軟體包commons-io-1.4.jar
Servlet主要利用fileupload軟體包中的介面和類來實現文件上傳,而fileupload軟體包本身依賴於io軟體包。
如果覺得可以,再把上傳的代碼發給你
『肆』 關於url類下載文件問題,拋異常: java.io.FileNotFoundException: 幫忙解決一下,謝謝!!
輸出流如果文件不存在,會自動生成。而輸入流則不會,而且會報FileNotFoundException ,你檢查一下你文件路徑是否正確。
『伍』 java 常用的什麼lang,io等包在哪裡下載
你把java環境配上就有了
『陸』 java文件下載的問題
循環是個死循環
『柒』 我在一個頁面下載文件時出現了這個提示:java.io.IOException: 系統資源不足,無法完成請求的服務。
readBytes()說明你的內存設置的小了,
『捌』 使用java的io流下載了一個網頁,但是打開時出現亂碼,求助!
你用java的IO讀寫的時候,記得設置參數裡面的編碼為下載網頁的編碼,要一致
『玖』 java 下載一個文件問什麼提示這個文件夾拒絕訪問,路徑沒錯,許可權沒錯就是簡單的IO流讀取的
java用輸入輸出流讀取文件的時候應該沒有關閉輸入輸出流,這樣的話文件就會一直被佔用, 重啟後肯定就可以再次訪問了。 一般java中io操作:
InputStream is = null;
try{
一些io操作
}catch(){}finally{
is.close();
}
『拾』 java項目如何製作下載的部分用io的話怎麼選則保存位置
有個servlet下載文件的例子,看看對你有沒有幫助。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
/**
*用於下載文件的servlet
*@vesion 1.0
*/
public class DownloadFile extends HttpServlet {
//字元編碼
private final String ENCODING="GB2312";
//內容類型
private final String CONTENT_TYPE="text/html;charset=GB2312";
//要下載的文件存放的路徑
private String downloadFileDir="download\\";
/**
*執行HTTP get操作
* @param req 從用戶來的請求
* @param resp servlet的回應
*/
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//設置request對象的字元編碼
request.setCharacterEncoding(ENCODING);
//從request 中取出要下載文件的名字
String fileName=request.getParameter("filename");
if(fileName==null || fileName.trim().equals("")){
//設置response對象ContentType
response.setContentType(CONTENT_TYPE);
//輸出錯誤信息
PrintWriter out=response.getWriter();
out.println("<font color=red>輸入的文件名無效!</font>");
out.close();
}
else{
//下載文件的完整路徑名
String fullFileName=downloadFileDir+fileName;
System.out.println("下載文件:"+fullFileName);
//根據文件的類型設置response對象ContentType
String contentType =getServletContext().getMimeType(fullFileName);
if(contentType==null)
contentType="application/octet-stream";
response.setContentType(contentType);
response.setHeader("content-disposition","attachment;filename=\""+fileName+"\"");
InputStream is=null;
OutputStream os=null;
try{
is=new BufferedInputStream(new FileInputStream(fullFileName));
//定義輸出位元組流
java.io.ByteArrayOutputStream baos=new ByteArrayOutputStream();
//定義response的輸出流
os =new BufferedOutputStream(response.getOutputStream());
//定義buffer
byte[] buffer=new byte[4*1024];//4k Buffer
int read =0;
//從文件中讀入數據並寫到輸出位元組流中
while ((read=is.read(buffer))!=-1){
baos.write(buffer,0,read);
}
//將輸出位元組流寫到response的輸出流中
os.write(baos.toByteArray());
}
catch (IOException e){
e.printStackTrace();
}
finally{
//關閉輸出位元組流和response輸出流
os.close();
is.close();
}
}
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
//調用doGet方法
doGet(req,resp);
}
}