jatarta fileupload 实例

jatarta fileupload 实例
<%
  //1.首先在http://jakarta.apache.org/commons/index.html下载FileUpload和IO的jar包,并放到WEB-INF/lib中;
  //2.如下代码:
%>
<%@page contentType="text/html; charset=GBK"%>
<%@page import="java.io.*,java.util.*,org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.servlet.* "%>
<html>
<head>
<title>upload</title>
</head>
<body bgcolor="#ffffff" style="font-family:Arial">
<h1>jakarta fileupload test</h1>
<form method="post" action="upload.jsp" enctype="multipart/form-data">
<br>
<br>
file:
<input type="file" name="file"/>
<br>
Txt:
<input type="text" name="Txt">
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
<%
  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  Map req = new HashMap();
  Map uploadFile = new HashMap();
  if (isMultipart) {
    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    // Parse the request
    List items = null; /* FileItem */
    try {
      items = upload.parseRequest(request);
    }
    catch (FileUploadException e) {
      out.println(e);
    }
    // Process the uploaded items
    Iterator iter = items.iterator();
    while (iter.hasNext()) {
      FileItem item = (FileItem) iter.next();
      if (item.isFormField()) {
        String name = item.getFieldName();
        String value = new String(item.getString().getBytes("ISO8859-1"), "GBK");
        req.put(name, value);
      }
      else {
        String fieldName = item.getFieldName();
        uploadFile.put(fieldName, item);
      }
    }
    items.clear();
  }
  if (!isMultipart) {
    return;
  }
  //TEST
  //request.getParameter("Txt");
  String key = "Txt";
  String value = req.get(key).toString();
  out.println(key + "=" + value);
  //save upload File "file"
  FileItem item = (FileItem) uploadFile.get("file1");
  out.print(item);
  if (item != null) {
    String filePath = item.getName(); //全路径文件名
    String fileName = filePath.replaceAll("", "/");
    fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
    String contentType = item.getContentType();
    boolean isInMemory = item.isInMemory();
    long sizeInBytes = item.getSize();
    File uploadedFile = new File("e:/" + fileName);
    try {
      item.write(uploadedFile);
    }
    catch (Exception e) {}
  }
%>

 


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=821981
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fileupload.aspx 是一个ASP.NET的页面,用于实现文件上传功能。该页面允许用户向服务器上传文件,并由服务器进行处理和保存。该页面一般包含以下功能或特点: 1. 文件选择:在Fileupload.aspx页面中,用户可以点击“选择文件”按钮,弹出文件选择对话框,从本地选择要上传的文件。用户可以选择单个文件或多个文件进行上传。 2. 文件类型过滤:在选择文件时,可以设置文件类型过滤,只允许上传指定类型的文件。例如,只允许上传图片文件(.jpg、.png等)或文档文件(.doc、.pdf等)。 3. 文件大小限制:可以在Fileupload.aspx页面中设置文件大小的限制,超过设定的大小限制将无法上传。这个限制可以根据实际需求进行设置,以避免上传过大的文件对服务器造成负担。 4. 文件上传进度条:在文件上传过程中,页面可以显示一个上传进度条,以提供用户上传进度的可视化展示。 5. 文件保存:一旦用户选择完文件并点击了上传按钮,Fileupload.aspx 页面会将选中的文件发送给服务器。服务器端代码会接收到文件并进行保存。文件可以保存在服务器的特定目录下,也可以保存在数据库中,具体保存方式可以根据需求进行选择。 总的来说,Fileupload.aspx 是一个用于文件上传的ASP.NET页面,可以方便地实现文件上传功能,并提供了一些常见的文件类型过滤、大小限制和上传进度条等功能,可以根据实际需求进行定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值