這個(gè)周末終于可以好好鍛煉一下我的IBM ThinkPad T43了。今天看了一些關(guān)于JSP,Servlet方面的資料,寫(xiě)了簡(jiǎn)單的兩個(gè)JavaBean。一個(gè)是UpLoad,一個(gè)是DownLoad。寫(xiě)得很簡(jiǎn)單,沒(méi)有使用其它任何組件,自己做的。大家可以來(lái)看看。
首先是RunningUpLoader.java,代碼有些多,因?yàn)槲沂亲约簛?lái)解析輸入流的。
package testupload;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
public class RunningUpLoader {
public RunningUpLoader() {
}
/**
* 上傳文件
* @param request HttpServletRequest Servlet的請(qǐng)求對(duì)象
* @param name String 要上傳的文件在表單中的參數(shù)名
* @param fileName String 保存在服務(wù)器上的文件名
* @throws IOException
*/
public void doUpload(HttpServletRequest request, String name,String fileName) throws
IOException {
InputStream in = request.getInputStream();
byte[] buffer = getFileContent(name,in);
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(buffer,0,buffer.length-2);
fos.close();
}
/**
* 獲取上傳的文件buffer,結(jié)尾處將多2個(gè)字節(jié)
* @param name String 文件上傳參數(shù)的名字
* @param is InputStream 服務(wù)器對(duì)話的輸入流
* @return byte[] 返回獲取的文件的buffer,結(jié)尾處將多2個(gè)字節(jié)
* @throws IOException
*/
private byte[] getFileContent(String name, InputStream is) throws IOException{
int index;
boolean isEnd = false;
byte[] lineSeparatorByte;
byte[] lineData;
String content_disposition;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
lineSeparatorByte = readStreamLine(bis);
while(!isEnd) {
lineData = readStreamLine(bis);
if(lineData == null) {
break;
}
content_disposition = new String(lineData,"ASCII");
index = content_disposition.indexOf("name=\"" + name + "\"");
if (index >= 0 && index < content_disposition.length()) {
readStreamLineAsString(bis); // skip a line
readStreamLineAsString(bis); // skip a line
while ((lineData = readStreamLine(bis)) != null) {
System.out.println(new String(lineData));
if (isByteArraystartWith(lineData, lineSeparatorByte)) { // end
isEnd = true;
break;
} else {
bos.write(lineData);
}
}
}else {
lineData = readStreamLine(bis);
if(lineData == null)
return null;
while(!isByteArraystartWith(lineData, lineSeparatorByte)) {
lineData = readStreamLine(bis);
if(lineData == null)
return null;
}
}
}
return bos.toByteArray();
}
private byte[] readStreamLine(BufferedInputStream in) throws IOException{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int b = in.read();
if(b== -1)
return null;
while(b != -1) {
bos.write(b);
if(b == ‘\n‘) break;
b = in.read();
}
return bos.toByteArray();
}
private String readStreamLineAsString(BufferedInputStream in) throws IOException {
return new String(readStreamLine(in),"ASCII");
}
private boolean isByteArraystartWith(byte[] arr,byte[] pat) {
int i;
if(arr == null || pat == null)
return false;
if(arr.length < pat.length)
return false;
for(i=0;i<pat.length;i++) {
if(arr[i] != pat[i])
return false;
}
return true;
}
}
上傳文件文件的時(shí)候,需要在之前的html中增加一個(gè)form表單,里面需要有個(gè)<input type="file" name="fileUpload" >的輸入按鈕。這樣,瀏覽器才會(huì)將要上傳的文件遞交給服務(wù)器。 其中name="fileUpload"的名字,就是RunnningUpLoader.doUpload函數(shù)中的name參數(shù)。
使用的時(shí)候直接用bean就OK了。比如upload.jsp如下:
<jsp:useBean id="upBean" scope="page" class="testupload.RunningUpLoader" />
<%
upBean.doUpload(request,"fileupload","runningUpLoad.txt");
%>
package testupload;
import java.io.*;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
public class RunningDownLoader {
public RunningDownLoader() {
}
public void doDownload(HttpServletResponse response, String filePath,String fileName) throws
IOException {
response.reset();//可以加也可以不加
response.setContentType("application/x-download");//設(shè)置為下載application/x-download
System.out.println(this.getClass().getClassLoader().getResource("/").getPath());
String filenamedownload = filePath;
String filenamedisplay = fileName;//系統(tǒng)解決方案.txt
filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload);
byte[] b = new byte[1024];
int i = 0;
while((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}
}
}
下載文件,最好是做到Servlet上。直接在Servlet的doGet下面增加如下代碼即可
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
RunningDownLoader downloader= new RunningDownLoader();
downloader.doDownload(response,"E:\\MyProjects\\testupload.rar","upload.bin");
}
聯(lián)系客服