上传文件的同时,将文件的基本信息添加到数据库中,例如文件名称,文件类型等。
这是jsp显示页面:
method = "post" action = "UploadServlet"name=form1>
上传文件: 红字体为重点!
下面是servlet
-
package control;
-
-
import java.io.*;
-
import java.sql.Date;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import db.DB;
-
import db.File1;
-
-
public class UploadServlet extends HttpServlet{
-
/**
-
*
-
*/
-
private static final long serialVersionUID = 1L;
-
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
//接收上传文件内容中临时文件的文件名
-
String tempFileName = new String("tempFileName");
-
//tempfile 对象指向临时文件
-
File tempFile = new File("D:/"+tempFileName);
-
//outputfile 文件输出流指向这个临时文件
-
FileOutputStream outputStream = new FileOutputStream(tempFile);
-
//得到客服端提交的所有数据
-
InputStream fileSourcel = request.getInputStream();
-
//将得到的客服端数据写入临时文件
-
byte b[] = new byte[1000];
-
int n ;
-
while ((n=fileSourcel.read(b))!=-1){
-
outputStream.write(b,0,n);
-
}
-
-
//关闭输出流和输入流
-
outputStream.close();
-
fileSourcel.close();
-
-
//randomFile对象指向临时文件
-
RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
-
//读取临时文件的第一行数据
-
randomFile.readLine();
-
//读取临时文件的第二行数据,这行数据中包含了文件的路径和文件名
-
String filePath = randomFile.readLine();
-
//得到文件名
-
int position = filePath.lastIndexOf('\\');
-
CodeToString codeToString = new CodeToString();
-
String filename = codeToString.codeString(filePath.substring(position,filePath.length()-1));
-
//重新定位读取文件指针到文件头
-
randomFile.seek(0);
-
//得到第四行回车符的位置,这是上传文件数据的开始位置
-
long forthEnterPosition = 0;
-
int forth = 1;
-
while((n=randomFile.readByte())!=-1&&(forth<=4)){
-
if(n=='\n'){
-
forthEnterPosition = randomFile.getFilePointer();
-
forth++;
-
}
-
}
-
-
//生成上传文件的目录
-
File fileupLoad = new File("F:/MyEclipse/Manager/WebRoot/file","upLoad");
-
fileupLoad.mkdir();
-
//saveFile 对象指向要保存的文件
-
File saveFile = new File("F:/MyEclipse/Manager/WebRoot/file/upLoad",filename);
-
RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
-
//找到上传文件数据的结束位置,即倒数第四行
-
randomFile.seek(randomFile.length());
-
long endPosition = randomFile.getFilePointer();
-
int j = 1;
-
while((endPosition>=0)&&(j<=4)){
-
endPosition--;
-
randomFile.seek(endPosition);
-
if(randomFile.readByte()=='\n'){
-
j++;
-
}
-
}
-
-
//从上传文件数据的开始位置到结束位置,把数据写入到要保存的文件中
-
randomFile.seek(forthEnterPosition);
-
long startPoint = randomFile.getFilePointer();
-
while(startPoint<endPosition){
-
randomAccessFile.write(randomFile.readByte());
-
startPoint = randomFile.getFilePointer();
-
}
-
-
String a=(String)request.getSession().getAttribute("user_name");
-
//Date ct=Date.valueOf(request.getParameter("wpublic_ct"));
-
String type =request.getParameter("file_type");
-
System.out.println(type);
-
File1 jBean=new File1();
-
//jBean.setWpublic_ct(ct);
-
jBean.setSales_id(a);
-
jBean.setFile_type(type);
-
jBean.setFile_name(filename);
-
String sql = "insert into File1( sales_id,file_type,file_name)values(?,?,?)";
-
DB d =new DB ();
-
int count =d.getInsertfile(sql, jBean);
-
if(count>0){
-
//关闭文件输入、输出
-
response.setCharacterEncoding("gbk");
-
PrintWriter out = response.getWriter();
-
out.print("");
-
-
-
}
-
-
-
randomAccessFile.close();
-
randomFile.close();
-
tempFile.delete(); }
-
-
-
-
}
下载:
">下载
采用超链接方式的下载 若直接单击下载超链接,很可能直接在网页打开了下载的页面。所以应该右键——目标另存为下载!!!
重点代码:
-
package control;
-
-
import java.io.*;
-
import java.net.URLEncoder;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import db.DB;
-
public class DownloadServlet extends HttpServlet{
-
/**
-
*
-
*/
-
private static final long serialVersionUID = 1L;
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException{
-
//获取要下载的文件名
-
String filename = "";
-
filename = URLEncoder.encode(filename,"UTF-8");
-
//得到想客服端输出的输出流
-
OutputStream outputStream = response.getOutputStream();
-
//输出文件用的字节数组,每次向输出流发送600个字节
-
byte b[] = new byte[600];
-
//要下载的文件
-
File fileload = new File("F:/MyEclipse/Manager/WebRoot/file/upLoad",filename);
-
//客服端使用保存文件的对话框
-
response.setHeader("Content-disposition", "attachment;filename="+filename);
-
//通知客服文件的MIME类型
-
response.setContentType("application/msword");
-
//通知客服文件的长度
-
long fileLength = fileload.length();
-
String length = String.valueOf(fileLength);
-
response.setHeader("Content_length", length);
-
//读取文件,并发送给客服端下载
-
FileInputStream inputStream = new FileInputStream(fileload);
-
int n = 0;
-
while((n=inputStream.read(b))!=-1){
-
outputStream.write(b,0,n);
-
}
-
-
response.setCharacterEncoding("gbk");
-
PrintWriter out = response.getWriter();
-
out.print("");
-
}
-
}
阅读(382) | 评论(0) | 转发(0) |