實現文件的上傳的第一種方式web
(經過servlet實現文件的上傳)瀏覽器
1:文件的上傳(以上傳圖片爲例)app
Jsp代碼:dom
<form action="UploadServlet" method="post"enctype="multipart/jsp
form-data">post
上傳文件:<input type="file" name="file1"><br>url
上傳文件:<input type="file" name="file2"><br>spa
上傳文件:<input type="file" name="file3"><br>pwa
${result};//顯示上傳結果指針
<input type="submit" value="提交">
</form><hr>
Servlet代碼:
public class UploadServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("已接收到請求");
//從request當中獲取流信息
InputStream fileSource = req.getInputStream();
String tempFileName = "E:/tempFile」;
//tempFile指向臨時文件
File tempFile = new File(tempFileName);
//outputStram文件輸出流指向這個臨時文件
FileOutputStream outStream = new FileOutputStream(tempFile);
byte b[] = new byte[1024];
int n;
while(( n = fileSource.read(b)) != -1){
outStream.write(b, 0, n);
}
//關閉輸出流、輸入流
outputStream.close();
fileSource.close();
//獲取上傳文件的名稱
RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
randomFile.readLine();//讀取臨時文件的第一行
String str = randomFile.readLine();//讀取臨時文件的第二行
//不一樣的瀏覽器下讀取文件的名稱的方法不同,能夠用httpwatch肯定
int begin = str.lastIndexOf("\\") + 1;
int end = str.lastIndexOf("\"");
String filename = str.substring(begin, end);
System.out.println("filename:" + filename);
//從新定位文件指針到文件頭
randomFile.seek(0);
long startPosition = 0;
int i = 1;
//獲取文件內容 開始位置
while(( n = randomFile.readByte()) != -1 && i <=4){
if(n == '\n'){
startPosition = randomFile.getFilePointer();
i ++;
}
}
startPosition = randomFile.getFilePointer() -1;
//獲取文件內容 結束位置
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 = endPosition -1;
//設置保存上傳文件的路徑
String realPath = getServletContext().getRealPath("/") + "images";
File fileupload = new File(realPath);
if(!fileupload.exists()){
fileupload.mkdir();
}
File saveFile = new File(realPath,filename);
RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
//從臨時文件當中讀取文件內容(根據起止位置獲取)
randomFile.seek(startPosition);
while(startPosition < endPosition){
randomAccessFile.write(randomFile.readByte());
startPosition = randomFile.getFilePointer();
}
//關閉輸入輸出流、刪除臨時文件
randomAccessFile.close();
randomFile.close();
tempFile.delete();
req.setAttribute("result", "上傳成功!");
RequestDispatcher dispatcher = req.getRequestDispatcher("index.jsp");
dispatcher.forward(req, resp);
}
}
2文件的下載
1:jsp代碼
下載:(txt.txt已放到images目錄下)
<a href="download.do?filename=text.txt">text.txt</a>${erro}
Servlet代碼:
public class Download extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path=getServletContext().getRealPath("/")+"images/";
String filename=req.getParameter("filename");
File file=new File(path+filename);
if(file.exists()){
//設置相應類型application/octet-stream
resp.setContentType("application/x-msdownload");
//設置頭信息
resp.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
InputStream inputStream = new FileInputStream(file);
ServletOutputStream ouputStream = resp.getOutputStream();
byte b[] = new byte[1024];
int n ;
while((n = inputStream.read(b)) != -1){
ouputStream.write(b,0,n);
}
//關閉流、釋放資源
ouputStream.close();
inputStream.close();
}
else{
req.setAttribute("erro", "文件不存在,下載失敗!");
RequestDispatcher dis=req.getRequestDispatcher("index.jsp");
dis.forward(req, resp);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
《web.xml文件生成,注意action與url-pattern 一致》