免费获取|
论文天下网
  • 论文天下网 |
  • 原创毕业论文 |
  • 论文范文 |
  • 论文下载 |
  • 计算机论文 |
  • 论文降重 |
  • 毕业论文 |
  • 外文翻译 |
  • 免费论文 |
  • 开题报告 |
  • 心得体会 |

当前位置:论文天下网 -> 免费论文 -> 计算机论文

网络多媒体资源管理信息系统的开发(五)

 
   else
   {
    session.setAttribute("user", userDTO);
  req.getRequestDispatcher("/WEB-INF/jsp/main.html").forward(req, resp);
   }
 }
}
 6. Filter
 package com.icss.mms.filter;
 /**
  * io包
  */
 import java.io.IOException;
 /**
  * servlet 包
  */
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 /**
  * 字符编码的过滤
  * @author WangHui
  */
 public class CharacterFilter implements Filter {
  private String encoding = null;  
  public void destroy() {
  }
  /**
   * 过滤器
   */
  Public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
   request.setCharacterEncoding(encoding);
   response.setCharacterEncoding(encoding);
   chain.doFilter(request, response);
  }  
  /**
   * 初始化
   */
  public void init(FilterConfig filterConfig) throws ServletException {
   encoding = filterConfig.getInitParameter("encoding");
  }
 }
 7. 文件上传
 package com.icss.mms.fileupload;
 /**
  * io包
  */
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 /**
  * util 包
  */
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 /**
  * servlet 包
  */
 import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
 /**
  * 上传文件
  * @author WangHui
  *
  */
 public class FileUploadModel {
 private static final long serialVersionUID = 2210691849912154951L;
 private static final String regFile = "Content-Disposition: form-data; name=\".*\"; filename=\".*\"";
 private static final String regArgu = "Content-Disposition: form-data; name=\".*\"";
 private static final int DEFAULT_SIZE = 1024;
 private byte[] buffer = null;
 private int size;
 private String fileName = null;
 private String boundaryString = null;
 private String endBoundaryString = null;
 private Map<String, String> map = new HashMap<String, String>();
  private List<String> paths = new ArrayList<String>();
  private String savePath = null;
  private BufferedOutputStream output = null;
  private ServletInputStream input = null;
  public FileUploadModel() {
   this.size = DEFAULT_SIZE;
  }
  public String getFileName() {
   return this.fileName;
  }
  public void setCacheSize(int size) {
   this.size = size;
  }
  /**
   * @param param
   * @return
   */
  public String getParemeter(String param) {
   String parameter = null;
   if (map.containsKey(param))
    parameter = map.get(param);
   return parameter;
  }
  /**
   * @param savePath
   */
  public void setSavePath(String savePath) {
   this.savePath = savePath;
  }
    /**
     * @param req
     * @throws Exception
     */
  public void upload(HttpServletRequest req) throws Exception {
   upload(req, null);
  }
  /**
   * @param req
   * @param savePath
   * @throws Exception
   */
  public void upload(HttpServletRequest req, String savePath)
    throws Exception {
   if (savePath != null)
    setSavePath(savePath);
    setBoundary(req);
    writeToFile(req);
  }
  /**
   * @param req
   */
  private void setBoundary(HttpServletRequest req) {
   String contentType = req.getHeader("content-type");
   int index = contentType.indexOf("=-") + 1;
   contentType = contentType.substring(index);
   boundaryString = "--" + contentType;
   endBoundaryString = boundaryString + "--";
  }
  /**
   * @throws SavePathException
   */
  private void checkSavePath() throws SavePathException {
   if (savePath == null || savePath.trim().equals("")) {
    throw new SavePathException("The savePath is invalid");
   }
  }
   /**
    * @return
    * @throws FileNotFoundException
    */
  private BufferedOutputStream createOutputStream()
    throws FileNotFoundException {
   BufferedOutputStream bos = null;
   String path =savePath + "/" + createUUID() + "_" + fileName;
   File outFile = new File(path);
   OutputStream fos = new FileOutputStream(outFile);
   bos = new BufferedOutputStream(fos);
   paths.add(path);
   return bos;
  }
    /**
     * @return
     */
  private String createUUID(){
   return UUID.randomUUID().toString();
  }
  /**
   * @param req
   * @throws IOException
   */
  private void createInputStream(HttpServletRequest req) throws IOException {
   input = req.getInputStream();
  }
  private void initBuffer() {
   buffer = new byte[size];
  }
  /**
   * 写入文件
   * @param req
   * @throws Exception
   */
  private void writeToFile(HttpServletRequest req) throws Exception {        checkSavePath();
   createInputStream(req);
   initBuffer();
   String fileContent = null;
   String line = null;
   int i = input.readLine(buffer, 0, buffer.length);
   line = new String(buffer, 0, i);
   boolean flag = true;
   boolean flagToWhile = true;
   while (i != -1) {
    flag = true;
    if (boundaryString.equals(line.trim())) {
     int j = input.readLine(buffer, 0, buffer.length);
     line = new String(buffer, 0, j);
     if (line.trim().matches(regFile)) {
      fileName = line.substring(
        line.trim().lastIndexOf("=\"") + 2, line.trim()
          .lastIndexOf("\""));
      input.readLine(buffer, 0, buffer.length);
      input.readLine(buffer, 0, buffer.length);
      j = input.readLine(buffer, 0, buffer.length);
      fileContent = new String(buffer, 0, j);
      /**
       * 验证请求头部是否有文件内容存在
       */
     if(fileContent != null && !fileContent.trim().equals("")) {
       flagToWhile = true;
       output = createOutputStream();
      } else {
       j = input.readLine(buffer, 0, buffer.length);
       line = new String(buffer, 0, j);
       flagToWhile = false;
       flag = false;
      }
      while (flagToWhile) {
if(boundaryString.equals(fileContent.trim())|| endBoundaryString.equals(fileContent.trim())) {
        line = fileContent;
        flag = false;
        output.flush();
        output.close();
        break;
       } else {
        output.write(buffer, 0, j);
       }
       j = input.readLine(buffer, 0, buffer.length);
       fileContent = new String(buffer, 0, j);
      }
     }
     if (line.trim().matches(regArgu)) {
      String name = line.substring(line.trim().indexOf("\"") + 1,
        line.trim().lastIndexOf("\""));
      input.readLine(buffer, 0, buffer.length);
      j = input.readLine(buffer, 0, buffer.length);
      String value = new String(buffer, 0, j).trim();
      map.put(name, value);
     }
    }
    if (flag) {
     i = input.readLine(buffer, 0, buffer.length);
     if (i != -1)
      line = new String(buffer, 0, i);
    }
   }
  }
 }
 8. 文件下载
 package com.icss.mms.filedownload;
 /**
  * io包
  */
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.OutputStream;
 /**
  * net包
  */
 import java.net.URLEncoder;
 /**
  * util 包
  */
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 /**
  * 文件下载
  * @author WangHui
  */
 public class FileDownLoadModel {
  private List<File> files;
  private ZipOutputStream out;
  /**
   * 下载模型
   * @param srcFiles
   * @param out
   */
  public FileDownLoadModel(File[] srcFiles, OutputStream out){
   this.files = new ArrayList<File>();
   addFile(srcFiles);
   this.out = new ZipOutputStream(out);
  }
  public void addFile(File[] files){
   for(int i = 0; i<files.length; i++){
    addFile(files[i]);
   }
  }
    /**
     * @param file
     */
  public void addFile(File file){
   this.files.add(file);
  }
  /**
   * 下载出压缩文件
   */
  public void writeZipFilesToStream(){
   byte[] b = new byte[1024];
   try{
 for(Iterator<File> ite = files.iterator(); ite.hasNext(); ){
   File file = ite.next();
 BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
ZipEntry zip = new ZipEntry(URLEncoder.encode(file.getName(), "UTF-8"));
     out.putNextEntry(zip);
     int length;
     while((length = in.read(b))!=-1){
      out.write(b,0,length);
     }
     out.closeEntry(); 
     in.close();
    }
    out.close();
   } catch(Exception e){
    e.printStackTrace();
    System.out.println("文件下载流异常中断!");
   }
  }
 }

首页 上一页 2 3 4 5 下一页 尾页 5/5/5

相关论文
上一篇:对等网络图像传输软件设计 下一篇:移动终端汉语拼音输入法及本地搜..
推荐论文 本专业最新论文
Tags:网络 多媒体 资源管理 信息系统 开发 【返回顶部】

相关栏目

自动化相关
计算机论文
工程管理论文
法律论文
医学论文
人力资源
电子专业
电气工程
英语论文
行政管理
电子商务
社科文学
教育论文
物流专业
金融专业
财务管理
会计专业
化学化工材料科学
电子通信
环境科学
经济类
机械模具类
报告,总结,申请书
其他专业论文


关于我们 | 联系方式 | 论文说明 | 网站地图 | 免费获取 | 钻石会员 | 原创毕业论文

 

论文天下网提供论文检测,论文降重,论文范文,论文排版,网站永久域名WWW.GEPUW.NET

本站部分文章来自网友投稿上传,如发现侵犯了您的版权,请联系指出,本站及时确认并删除  E-mail: 893628136@qq.com

Copyright@ 2009-2022 GEPUW.NET 论文天下网 版权所有