简单文件上转

 1.java文件

package com.topsoft.cya.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;

public class FileUploadServlet extends HttpServlet {

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to
  * post.
  *
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=gbk");
  PrintWriter out = response.getWriter();

  try {
   String path, callback;
   int maxSize = 10 * 1024 * 1024;
   SmartUpload upload = new SmartUpload();
   upload.initialize(getServletConfig(), request, response);
   upload.setAllowedFilesList("bmp,jpg,gif,jpeg,png,swf");
   upload.setMaxFileSize(maxSize);

   // 上载文件
   upload.upload();
   // 取出参数
   Request r = upload.getRequest();
   path = r.getParameter("path");
   callback = r.getParameter("callback");
   Files fs = upload.getFiles();

   if (fs.getCount() > 0) {
    File f = fs.getFile(0);
    if (!f.isMissing()) {
     // 文件上传
     String _path = "uploadfile/" + path;
     String root = getServletContext().getRealPath("/");
     if (!_path.endsWith("/"))
      _path += "/";

     java.io.File _f = new java.io.File(root + _path);
     if (!_f.exists()) {
      _f.mkdirs();
     }
     _f = null;

     _path += getFileName() + "." + f.getFileExt();

     f.saveAs(root + _path);
     out.println("<script type=\"text/javascript\">parent."
       + callback + "(\"" + _path
       + "\");window.history.back();</script>");
    }
   }
  } catch (Exception e) {
   out
     .println("<script type=\"text/javascript\">alert(\"文件上传失败:\\n"
       + "上传文件格式必须为(jpg,png,bmp,gif,swf),\\n文件大小不允许超过10M\");window.history.back();</script>");
  }

  out.flush();
  out.close();
 }

 private String getFileName() {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  String ret = sdf.format(new Date());
  Random r = new Random();
  ret += r.nextInt(100) + 100;
  return ret;
 }
}

2.web.xml 配置

 <servlet>
    <description>文件上传</description>
    <display-name>文件上传</display-name>
    <servlet-name>FileUploadServlet</servlet-name>
    <servlet-class>com.topsoft.cya.web.servlet.FileUploadServlet</servlet-class>
  </servlet>
   <servlet-mapping>
    <servlet-name>FileUploadServlet</servlet-name>
    <url-pattern>/Upload</url-pattern>
  </servlet-mapping>

 

3.upload.jsp

<%@page contentType="text/html;charset=GBK"%>
<%
String rootPath = request.getContextPath();
%>
<html>
 <link rel="stylesheet" type="text/css"
  href="<%=rootPath%>/etc/styles/cya.css" />
 <style type="text/css">
<!--
body,td,th {
 font-size: 12px;
}

body {
 margin: 0px;
 padding: 0px;
 overflow: hidden;
}
-->
</style>
 <script type="text/javascript">
 
   function validate()
   {
     var f=document.forms[0];
     if(f.file1.value.length == 0)
     {
       alert("文件名不能为空");
       return;
     }
     else{
        f.submit();
     }
   }
 
 </script> 
 

 <body>
 
    
  <form action="<%=rootPath%>/Upload" method="post"
   enctype="multipart/form-data">
   <table>
    <tr>
     <input type="hidden" name="path"
      value="<%=request.getParameter("path")%>">
     <input type="hidden" name="callback"
      value="<%=request.getParameter("callback")%>">
     <td>
      <input type="file" id="file1"  name="file1"  size="50"/>
     </td>
     <td>
      <input type="button" class="button2" name="Submit" value="上传" οnclick="validate();" />
     </td>
    </tr>
   </table>
  </form>
 </body>
</html>

 

4.test.html

<html>
 <body>
  <script type="text/javascript">
      function onUpload(f){
        alert("文件已经保存到:"+f);
      }
 
     </script>
  <iframe src="upload.jsp?path=news&callback=onUpload" width="460"
   height="32" marginheight="0" marginwidth="0" frameborder="0"></iframe>
 </body>
</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值