打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
jsp页面文件上传
    <%@ page contentType="text/html; charset=utf-8" %>
    < %@ page import="java.io.*"%>
    < %@ page import="java.util.*"%>
    < %@ page import="java.text.SimpleDateFormat"%>
    < meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    < %
    int MAX_SIZE = 1024 * 1024 * 1;

    String rootPath = request.getRealPath("/") + "/upload/";
    String contentType=request.getContentType();

    try{
    if(contentType.indexOf("multipart/form-data") >= 0){

    DataInputStream in = new DataInputStream(request.getInputStream());
    int formDataLength = request.getContentLength();
    if(formDataLength > MAX_SIZE){
    out.println("<script>alert('上传的文件字节数不可以超过" + MAX_SIZE + "');window.history.go(-1);</script>");
    return;
    }
    byte dataBytes[] = new byte[formDataLength];
    int byteRead = 0;
    int totalBytesRead = 0;
    while(totalBytesRead < formDataLength){
    byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
    totalBytesRead += byteRead;
    }
    String file = new String(dataBytes);
    String oldFileName = file.substring(file.indexOf("filename=\"") + 10);
    oldFileName = oldFileName.substring(0,oldFileName.indexOf("\n"));
    oldFileName = oldFileName.substring(oldFileName.lastIndexOf("/") + 1,oldFileName.indexOf("\""));
    String sExt = oldFileName.substring(oldFileName.lastIndexOf("."),oldFileName.length());

    java.util.Date dt = new java.util.Date(System.currentTimeMillis());
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    String nenFileName= fmt.format(dt) + sExt;
    String fileName = rootPath + nenFileName;

    int lastIndex = contentType.lastIndexOf("=");
    String boundary = contentType.substring(lastIndex + 1,contentType.length());

    int pos;
    pos = file.indexOf("filename=\"");
    pos = file.indexOf("\n",pos) + 1;
    pos = file.indexOf("\n",pos) + 1;
    pos = file.indexOf("\n",pos) + 1;
    int boundaryLocation = file.indexOf(boundary,pos) - 4;
    int startPos = ((file.substring(0,pos)).getBytes()).length;
    int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;

    File fileDir = new File(rootPath);
    if(!fileDir.exists()){
    fileDir.mkdirs();
    }

    FileOutputStream fileOut = new FileOutputStream(fileName);
    fileOut.write(dataBytes,startPos,(endPos - startPos));
    fileOut.close();
    out.println("<script>alert('上传成功!')</script>");
    }else{
    String content = request.getContentType();
    out.println("<p>上传的数据类型不是multipart/form-data</p>");
    }
    }catch(Exception ex){
    throw new ServletException(ex.getMessage());
    }
    %>
      import java.text.NumberFormat;

      public class Test {
      public static String numerFormat(double d) {
      NumberFormat nf=NumberFormat.getNumberInstance();
      nf.setMaximumFractionDigits(2);
      return nf.format(d);
      }
      }
    本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
    打开APP,阅读全文并永久保存 查看更多类似文章
    猜你喜欢
    类似文章
    【热】打开小程序,算一算2024你的财运
    纯jsp的自定义的单个文件上载代码
    用JSP实现文件上传功能
    用Servlvet实现文件上传的功能
    采用HTTP协议上传文件实现(java)
    Java多线程 Web服务器简单实现
    接着昨天的,一个分析网页源代码提取股票数据的程序
    更多类似文章 >>
    生活服务
    热点新闻
    分享 收藏 导长图 关注 下载文章
    绑定账号成功
    后续可登录账号畅享VIP特权!
    如果VIP功能使用有故障,
    可点击这里联系客服!

    联系客服