COS文件上传

upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <base href="<%=basePath%>">

    <title>文件上传</title>

 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
  </head>
 <body>
  <form action="foo/receiver.jsp" enctype="multipart/form-data" method="post">
   <table align="center">
    <tr><td>姓名</td><td><input type="text" name="name" value = "花花"/></td></tr>
    <tr><td>性别</td><td><input type="text" name="sex" value="男"/></td></tr>
    <tr><td>年龄</td><td><input type="text" name="age" value="108"/></td></tr>
    <tr><td>文件1</td><td><input type="file" name="file" value=""/></td></tr>
    <tr><td>文件2</td><td><input type="file" name="file" value=""/></td></tr>
    <tr><td>文件3</td><td><input type="file" name="file" value=""/></td></tr>
    <tr><td>文件4</td><td><input type="file" name="file" value=""/></td></tr>
    <tr><td colspan="2"><input type="submit" value ="提交"/></td></tr>
   </table>
  </form>
  </body>
</html>





receiver.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import ="com.oreilly.servlet.multipart.MultipartParser,com.oreilly.servlet.multipart.Part" %>
<jsp:directive.page import="com.oreilly.servlet.multipart.ParamPart"/>
<jsp:directive.page import="com.oreilly.servlet.multipart.FilePart"/>
<jsp:directive.page import="java.io.InputStream"/>
<jsp:directive.page import="java.io.BufferedOutputStream"/>
<jsp:directive.page import="java.io.OutputStream"/>
<jsp:directive.page import="java.io.FileOutputStream"/>
<jsp:directive.page import="java.io.File"/>
<%
 MultipartParser mp = new MultipartParser(request,1024*1024*10);
 mp.setEncoding("utf-8");
 Part part;

 while((part=mp.readNextPart())!=null){
  String name = part.getName();

  if(part.isParam()){
   ParamPart pp = (ParamPart)part;
   String value = pp.getStringValue();
   out.println(name+"="+value);
  }
  else if(part.isFile()){
   FilePart fp = (FilePart)part;
   String filename = fp.getFileName();
   String fileType = fp.getContentType();
   String filePath = fp.getFilePath();
   InputStream is = fp.getInputStream();
   FileOutputStream fos = new FileOutputStream(new File("e:/"+filename));
   BufferedOutputStream bos = new BufferedOutputStream(fos);
   byte[] bt = new byte[2048];
   int k ;
   while(( k  =is.read(bt))>0){
    bos.write(bt,0,k);
   }
   out.println("文件名称:"+filename);
   out.println("<br/>文件类型"+fileType);
   out.println("<br/>文件路径:"+filePath);

  }else{
   out.println("File name:"+name);
  }
 }

%>


你可能感兴趣的:(java,jsp,servlet,FP)