-----------------------------7d71042a40328
Content-Disposition: form-data; name="fileforload"; filename="C:\Documents and Settings\ZJ\桌面\book.txt"
Content-Type: text/plain
//此处为文件内容
-----------------------------7d71042a40328
Content-Disposition: form-data; name="submit"
commit
-----------------------------7d71042a40328--
|
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=GB18030">
<title>This page for FileUploadtitle>
head>
<body>
<p>Choose the file for uploading:
<form action="accept.jsp"method=postenctype="multipart/form-data">
<input type=filename=fileforloadsize=30>
<br>
<input type=submitvalue=commitname=submit>
form>
body>
html>
|
<html>
<head>
<%@ page language="java"import="java.io.*"%>
<metahttp-equiv="Content-Type"content="text/html; charset=GB18030">
<title>This page for responsetitle>
head>
<body>
<%
try {
if (request.getContentLength() > 297) {
InputStream in = request.getInputStream();
File f =new File("d:/temp","test.txt");
FileOutputStream o =new FileOutputStream(f);
byte b[] =newbyte[1024];
int n;
while ((n = in.read(b)) != -1) {
o.write(b, 0, n);
}
o.close();
in.close();
out.print("File upload success!");
}else {
out.print("No file!");
}
} catch (IOException e) {
out.print("upload error.");
e.printStackTrace();
}
%>
body>
html>
|
-----------------------------7d75b1540328
Content-Disposition: form-data; name="fileforload"; filename="C:\Documents and Settings\ZJ\桌面\I like.txt"
Content-Type: text/plain
我喜欢驾驭着代码在风驰电掣中创造完美;
我喜欢操纵着代码在随心所欲中体验生活;
我喜欢用心情代码编制我小小的与众不同;
每一段新的代码在我手中延生对我来说就象观看刹那花开的感动;
我不需要焦点.因为我就是焦点!
-----------------------------7d75b1540328
Content-Disposition: form-data; name="submit"
commit
-----------------------------7d75b1540328--
|
<html>
<head>
<%@ page language="java"import="java.io.*"%>
<metahttp-equiv="Content-Type"content="text/html; charset=GB18030">
<title>The real filetitle>
head>
<body>
<%try{
//use sessionid to create a temp file.
String tempFileName=(String)session.getId();
//create the temp file.
File temp=new File("d:/temp",tempFileName);
FileOutputStream o=new FileOutputStream(temp);
if(request.getContentLength()>297){
//write the upload content to the temp file.
InputStream in=request.getInputStream();
byte b[]=newbyte[1024];
int n;
while((n=in.read(b))!=-1){
o.write(b,0,n);
}
o.close();
in.close();
//read the temp file.
RandomAccessFile random=new RandomAccessFile(temp,"r");
//read Line2 to find the name of the upload file.
int second=1;
String secondLine=null;
while(second<=2){
secondLine=random.readLine();
second++;
}
//get the last location of the dir char.'\\'.
int position=secondLine.lastIndexOf('\\');
//get the name of the upload file.
String fileName=secondLine.substring(position+1,secondLine.length()-1);
//relocate to the head of file.
random.seek(0);
//get the location of the char.'Enter' in Line4.
long forthEndPosition=0;
int forth=1;
while((n=random.readByte())!=-1&&(forth<=4)){
if(n=='\n'){
forthEndPosition=random.getFilePointer();
forth++;
}
}
File realFile=new File("d:/temp",fileName);
RandomAccessFile random2=new RandomAccessFile(realFile,"rw");
//locate the end position of the content.Count backwards 6 lines.
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6)){
mark--;
random.seek(mark);
n=random.readByte();
if(n=='\n'){
endPosition=random.getFilePointer();
j++;
}
}
//locate to the begin of content.Count for 4 lines's end position.
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
//read the real content and write it to the realFile.
while(startPoint
n=random.readByte();
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();
random.close();
//delete the temp file.
temp.delete();
out.print("File upload success!");
}
else{
out.print("No file!");
}
}
catch(IOException e){
out.print("upload error.");
e.printStackTrace();
}
%>
body>
html>
|
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=GB18030">
<title>download pagetitle>
head>
<body>
<a href=loadFile>Download:test.zipa>
body>
html>
|
package com.zj.sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
publicclass LoadFileextends HttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
OutputStream o = response.getOutputStream();
byte b[] =newbyte[1024];
// the file to download.
File fileLoad = new File("d:/temp","test.rar");
// the dialogbox of download file.
response.setHeader("Content-disposition","attachment;filename="
+"test.rar");
// set the MIME type.
response.setContentType("application/x-tar");
// get the file length.
long fileLength = fileLoad.length();
String length = String.valueOf(fileLength);
response.setHeader("Content_Length", length);
// download the file.
FileInputStream in = new FileInputStream(fileLoad);
int n = 0;
while ((n = in.read(b)) != -1) {
o.write(b, 0, n);
}
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
doGet(request, response);
}
}
|
<servlet>
<servlet-name>LoadFileServletservlet-name>
<servlet-class>com.zj.sample.LoadFileservlet-class>
servlet>
<servlet-mapping>
<servlet-name>LoadFileServletservlet-name>
<url-pattern>/loadFileurl-pattern>
servlet-mapping>
|
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/19631