码迷,mamicode.com
首页 > Web开发 > 详细

jsp实现文件上传(二)用cos组件实现文件上传

时间:2014-12-08 17:28:41      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   使用   sp   for   

jsp表单

<%@ page language="java" pageEncoding="utf-8"%>

<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>

<body>
    <form action="cos.jsp" method="post" enctype="multipart/form-data">
        <input type="text" name="info1"><br> 
        <input type="text" name="info2"><br> 
        <input type="file" name="userface1"><br> 
        <input type="file" name="userface2"><br> 
        <input type="file" name="userface3"><br> 
        <input type="submit" name="mysub" value="upfile">
    </form>
</body>
</html>

cos组件实现文件上传 jsp代码

<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.io.File"%>
<%@ page import="com.oreilly.servlet.multipart.FilePart"%>
<%@ page import="com.oreilly.servlet.multipart.ParamPart"%>
<%@ page import="com.oreilly.servlet.multipart.Part"%>
<%@ page import="com.oreilly.servlet.multipart.MultipartParser"%>
<%
    //使用cos组件实现文件上传
    MultipartParser mp = new MultipartParser(request, 10 * 1024 * 1024);
    Part part;
    mp.setEncoding("utf-8");//取决于来源页面的编码,如果是表态的应为gbk,如果是jsp应为utf-8
    int ii = 0;
    while ((part = mp.readNextPart()) != null) {
        String name = part.getName();
        if (part.isParam()) {
            ParamPart paramPart = (ParamPart) part;
            String value = paramPart.getStringValue();
            out.println("param: name=" + name + "; value=" + value);
            System.out.println(value);
        } else if (part.isFile()) {
            FilePart filePart = (FilePart) part;
            String fileName = filePart.getFileName();
            if (fileName != null) {
                long size = filePart.writeTo(new File(this.getServletContext().getRealPath("/upload")));
                out.println("file: name=" + name + "; fileName=" + fileName +
                  ", filePath=" + filePart.getFilePath() + 
                  ", contentType=" + filePart.getContentType() + 
                  ", size=" + size);
              }else { 
                out.println("file: name=" + name + "; EMPTY");
              }
            out.print(fileName);
        }
    }
%>

文件命名机制 

1.根据uuid

import java.io.File;
import java.util.UUID;

import com.oreilly.servlet.multipart.FileRenamePolicy;

public class MyUuid implements FileRenamePolicy {

    @Override
    public File rename(File f) {
        String body = UUID.randomUUID().toString();
        String ext = "";
        int pot = f.getName().lastIndexOf(".");
        if (pot != -1) {
            ext = f.getName().substring(pot);
        }
        String newName = body + ext;
        f = new File(f.getParent(), newName);
        return f;
    }

}

2.根据时间随机数

import java.io.File;
import java.util.UUID;

import com.oreilly.servlet.multipart.FileRenamePolicy;

public class MyRandom implements FileRenamePolicy {

    @Override
    public File rename(File f) {
        String body = String.valueOf(System.currentTimeMillis());
        String ext = "";
        int pot = f.getName().lastIndexOf(".");
        if (pot != -1) {
            ext = f.getName().substring(pot);
        }
        String newName = body + ext;
        f = new File(f.getParent(), newName);
        return f;
    }

}

上传文件并改名字

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>cc.html</title>
  </head>
  
  <body>
  <form action="cc.jsp" method="post" enctype="multipart/form-data">
  姓名:<input type="text" name="uname"><br>
 文件1:<input type="file" name="ufile1"><br>
 文件2:<input type="file" name="ufile2"><br>
 <input type="submit" value="提交">
  </form>
  
  </body>
</html>

 

<%@page import="com.MyRandom"%>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@ page import="com.MyUuid"%>
<%@ page import="java.io.File"%>
<%
    //使用cos组件实现文件上传
    String path = this.getServletContext().getRealPath("/upload");
    //MultipartRequest req = new MultipartRequest(request, path, "utf-8");
    //out.print(req.getParameter("uname"));
    //out.print("<hr>");
    //out.print(req.getFile("ufile1").getName());
    //out.print(req.getFile("ufile2").getName());
    
    //MultipartRequest req = new MultipartRequest(request,path,10*1024*1024,"utf-8",new MyUuid());
    MultipartRequest req = new MultipartRequest(request,path,10*1024*1024,"utf-8",new MyRandom());
    out.print(req.getFile("ufile1").getName());
    
%>

 

jsp实现文件上传(二)用cos组件实现文件上传

标签:style   blog   io   ar   color   os   使用   sp   for   

原文地址:http://www.cnblogs.com/Dreamlu/p/4151312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!