网站地图| 免费获取|
毕业论文网
  • 网站首页|
  • 论文范文|
  • 论文降重|
  • 职称论文发表|
  • 合作期刊|
  • 论文下载|
  • 计算机论文|
  • 外文翻译|
  • 免费论文|
  • 论文资料|
  • 论文开题报告
搜索

当前位置:毕业论文网 -> 免费论文 -> 计算机论文 -> 网络多媒体资源管理信息系统的开发(五)
计算机论文资料| ASP设计| Delphi| VB设计| JSP设计| ASP.NET设计| VB.NET| java设计| VC| pb| VS| dreamweaver| c#.net| vf| VC++| 计算机论文范文| 论文下载| 自动化论文

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

最新活动:微信集50个赞就可获取任意一篇钻石会员文档。详情见微信集赞换文档
网络多媒体资源管理信息系统的开发(五)  
   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

网络多媒体资源管理信息系统的开发(五)由毕业论文网(www.huoyuandh.com)会员上传。
原创论文资料流程 相关论文
上一篇:对等网络图像传输软件设计 下一篇:移动终端汉语拼音输入法及本地搜..
推荐论文 本专业最新论文
Tags:网络 多媒体 资源管理 信息系统 开发 2011-01-15 10:31:37【返回顶部】
精彩推荐
发表论文

联系方式 | 论文说明 | 网站地图 | 免费获取 | 钻石会员 | 硕士论文资料


毕业论文网提供论文范文,论文代发,原创论文资料

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

Copyright@ 2009-2020 毕业论文网 版权所有