Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49367
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-10 15:23
文章分类
文章存档

2014年(1)

2013年(16)

我的朋友

分类: Java

2013-06-26 10:11:23

 上传文件的同时,将文件的基本信息添加到数据库中,例如文件名称,文件类型等。
这是jsp显示页面:
enctype="multipart/form-data"
method = "post" action = "UploadServlet"name=form1>
上传文件: 红字体为重点!
下面是servlet

点击(此处)折叠或打开

  1. package control;

  2. import java.io.*;
  3. import java.sql.Date;

  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;

  8. import db.DB;
  9. import db.File1;

  10. public class UploadServlet extends HttpServlet{
  11.     /**
  12.      *
  13.      */
  14.     private static final long serialVersionUID = 1L;

  15.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  16.     throws ServletException, IOException {
  17.         //接收上传文件内容中临时文件的文件名
  18.         String tempFileName = new String("tempFileName");
  19.         //tempfile 对象指向临时文件
  20.         File tempFile = new File("D:/"+tempFileName);
  21.         //outputfile 文件输出流指向这个临时文件
  22.         FileOutputStream outputStream = new FileOutputStream(tempFile);
  23.         //得到客服端提交的所有数据
  24.         InputStream fileSourcel = request.getInputStream();
  25.         //将得到的客服端数据写入临时文件
  26.         byte b[] = new byte[1000];
  27.         int n ;
  28.         while ((n=fileSourcel.read(b))!=-1){
  29.          outputStream.write(b,0,n);
  30.         }
  31.         
  32.         //关闭输出流和输入流
  33.         outputStream.close();
  34.         fileSourcel.close();
  35.         
  36.         //randomFile对象指向临时文件
  37.         RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
  38.         //读取临时文件的第一行数据
  39.         randomFile.readLine();
  40.         //读取临时文件的第二行数据,这行数据中包含了文件的路径和文件名
  41.         String filePath = randomFile.readLine();
  42.         //得到文件名
  43.         int position = filePath.lastIndexOf('\\');
  44.         CodeToString codeToString = new CodeToString();
  45.         String filename = codeToString.codeString(filePath.substring(position,filePath.length()-1));
  46.         //重新定位读取文件指针到文件头
  47.         randomFile.seek(0);
  48.         //得到第四行回车符的位置,这是上传文件数据的开始位置
  49.         long forthEnterPosition = 0;
  50.         int forth = 1;
  51.         while((n=randomFile.readByte())!=-1&&(forth<=4)){
  52.          if(n=='\n'){
  53.          forthEnterPosition = randomFile.getFilePointer();
  54.          forth++;
  55.          }
  56.         }
  57.         
  58.             //生成上传文件的目录
  59.         File fileupLoad = new File("F:/MyEclipse/Manager/WebRoot/file","upLoad");
  60.         fileupLoad.mkdir();
  61.         //saveFile 对象指向要保存的文件
  62.         File saveFile = new File("F:/MyEclipse/Manager/WebRoot/file/upLoad",filename);
  63.         RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
  64.         //找到上传文件数据的结束位置,即倒数第四行
  65.         randomFile.seek(randomFile.length());
  66.         long endPosition = randomFile.getFilePointer();
  67.         int j = 1;
  68.         while((endPosition>=0)&&(j<=4)){
  69.          endPosition--;
  70.          randomFile.seek(endPosition);
  71.          if(randomFile.readByte()=='\n'){
  72.          j++;
  73.          }
  74.         }
  75.         
  76.         //从上传文件数据的开始位置到结束位置,把数据写入到要保存的文件中
  77.         randomFile.seek(forthEnterPosition);
  78.         long startPoint = randomFile.getFilePointer();
  79.         while(startPoint<endPosition){
  80.          randomAccessFile.write(randomFile.readByte());
  81.          startPoint = randomFile.getFilePointer();
  82.         }
  83.         
  84.         String a=(String)request.getSession().getAttribute("user_name");
  85.         //Date ct=Date.valueOf(request.getParameter("wpublic_ct"));
  86.         String type =request.getParameter("file_type");
  87.         System.out.println(type);
  88.         File1 jBean=new File1();
  89.         //jBean.setWpublic_ct(ct);
  90.         jBean.setSales_id(a);
  91.         jBean.setFile_type(type);
  92.         jBean.setFile_name(filename);
  93.         String sql = "insert into File1( sales_id,file_type,file_name)values(?,?,?)";
  94.         DB d =new DB ();
  95.         int count =d.getInsertfile(sql, jBean);
  96.         if(count>0){
  97.         //关闭文件输入、输出
  98.         response.setCharacterEncoding("gbk");
  99.         PrintWriter out = response.getWriter();
  100.         out.print("");
  101.         
  102.         
  103.         }
  104.         
  105.         
  106.         randomAccessFile.close();
  107.         randomFile.close();
  108.         tempFile.delete(); }
  109.     
  110.     
  111.     
  112.     }

下载:

    ">下载
 
采用超链接方式的下载 若直接单击下载超链接,很可能直接在网页打开了下载的页面。所以应该右键——目标另存为下载!!!

重点代码:

点击(此处)折叠或打开

  1. package control;

  2. import java.io.*;
  3. import java.net.URLEncoder;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;

  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;

  10. import db.DB;
  11. public class DownloadServlet extends HttpServlet{
  12.     /**
  13.      *
  14.      */
  15.     private static final long serialVersionUID = 1L;
  16.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  17.     throws ServletException, IOException{
  18.     //获取要下载的文件名
  19.     String filename = "";
  20.     filename = URLEncoder.encode(filename,"UTF-8");
  21.     //得到想客服端输出的输出流
  22.     OutputStream outputStream = response.getOutputStream();
  23.     //输出文件用的字节数组,每次向输出流发送600个字节
  24.     byte b[] = new byte[600];
  25.     //要下载的文件
  26.     File fileload = new File("F:/MyEclipse/Manager/WebRoot/file/upLoad",filename);
  27.     //客服端使用保存文件的对话框
  28.     response.setHeader("Content-disposition", "attachment;filename="+filename);
  29.     //通知客服文件的MIME类型
  30.     response.setContentType("application/msword");
  31.     //通知客服文件的长度
  32.     long fileLength = fileload.length();
  33.     String length = String.valueOf(fileLength);
  34.     response.setHeader("Content_length", length);
  35.     //读取文件,并发送给客服端下载
  36.     FileInputStream inputStream = new FileInputStream(fileload);
  37.     int n = 0;
  38.     while((n=inputStream.read(b))!=-1){
  39.         outputStream.write(b,0,n);
  40.     }
  41.  
  42.     response.setCharacterEncoding("gbk");
  43.     PrintWriter out = response.getWriter();
  44.     out.print("");
  45. }
  46.     }
阅读(397) | 评论(1) | 转发(2) |
给主人留下些什么吧!~~