jsp實現檔案上傳(二)用cos組件實現檔案上傳

來源:互聯網
上載者:User

標籤: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組件實現檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.