日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java jsp 文件上传_JSP实现快速上传文件的方法

發布時間:2023/12/10 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java jsp 文件上传_JSP实现快速上传文件的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講述了JSP實現快速上傳文件的方法。分享給大家供大家參考。具體如下:

這里演示JSP不使用第三方庫,實現快速上傳文件的功能

1. FileUpload.java:

package FileUpload;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import javax.servlet.ServletInputStream;

/**

*

*/

/**

* @author Qch

*

*/

public class FileUpload

{

ServletInputStream in=null;

String fpath="C://";

public FileUpload()

{

fpath="C://";

in=null;

}

public void setInputStream(ServletInputStream in)

{

this.in=in;

}

public void setFpath(String p)

{

this.fpath=p;

}

public String getFpath()

{

return fpath;

}

public String getParameter()

{

String r=null;

try

{

r=getParameter(in);

}

catch (Exception e)

{

e.printStackTrace();

}

return r;

}

public long getFileUpload()

{

long r=-1;

try

{

r=getFileUpload(in,fpath);

}

catch (Exception e)

{

e.printStackTrace();

}

return r;

}

public String getParameter(ServletInputStream in)// 只能按順序提取

throws Exception

{

int l = 0;

byte[] b = new byte[1024];

l = in.readLine(b, 0, b.length);// 依次是讀取屬性的開始符、名稱、屬性值的類型、屬性的值

String si = new String(b);

if (si.startsWith("----------------------------"))

{// 表示是從開始符開始讀,否則應為剛讀取文件后的一個屬性,此時應少讀一次

l = in.readLine(b, 0, b.length);

}

l = in.readLine(b, 0, b.length);

l = in.readLine(b, 0, b.length);

String value = new String(b, 0, l);

return value;

}

public long getFileUpload(ServletInputStream in, String fpath)// 需要提供輸入流和存儲路徑

throws Exception

{

// out.println("文件信息:
");

long begin = System.currentTimeMillis();// 傳送時間計時開始

int l = 0;

byte[] b = new byte[1024];

l = in.readLine(b, 0, b.length);

String sign = new String(b, 0, l);// eg.-----------------------------7d9dd29630a34

l = in.readLine(b, 0, b.length);

String info = new String(b, 0, l);// eg.Content-Disposition:form-data;

// name="file";

l = in.readLine(b, 0, b.length);

// String type=new

// String(b,0,l);//eg.Content-Type:application/octet-stream(程序文件)

l = in.readLine(b, 0, b.length);

// String nulll=new String(b,0,l);//此值應為空

int nIndex = info.toLowerCase().indexOf("filename=\"");

int nLastIndex = info.toLowerCase().indexOf("\"", nIndex + 10);

String filepath = info.substring(nIndex + 10, nLastIndex);

int na = filepath.lastIndexOf("\\");

String filename = filepath.substring(na + 1);

// out.println("文件絕對路徑:"+filepath+"
");

// out.println("文件名:"+filename+"
");

String path=fpath + filename;

File fi = new File(path);// 建立目標文件

if (!fi.exists()&&!fi.createNewFile())

return -2;

BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream(

fi));

while ((l = in.readLine(b, 0, b.length)) > 0)

{

if (l == sign.length())

{

String sign1 = new String(b, 0, sign.length());

// out.println(sign1+"
");

if (sign1.startsWith(sign))// 比對是否文件已傳完

break;

}

f.write(b, 0, l);

f.flush();

}

f.flush();

f.close();

long end = System.currentTimeMillis();// 傳送時間計時結束

// out.println("上傳文件用時:"+(end-begin)+"毫秒
");

return end - begin;

}

}

這就是微學網-程序員之家為你提供的"JSP實現快速上傳文件的方法"希望對你有所幫助.本文來自網絡,轉載請注明出處:http://www.weixuecn.cn/article/4862.html

總結

以上是生活随笔為你收集整理的java jsp 文件上传_JSP实现快速上传文件的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。