jatarta fileupload 实例

简介: uploadjakarta fileupload testfile:Txt:
<%
  //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) {}
  }
%>
目录
相关文章
|
5月前
|
Java Apache Windows
commons-fileupload组件和commons-io组件的详细下载
这篇文章提供了Apache Commons FileUpload和Commons IO组件的详细下载方法,包括下载地址和如何从压缩包中获取相应的jar类库文件。
|
7月前
apache.commons.lang3常用工具类
apache.commons.lang3常用工具类
61 2
|
移动开发 前端开发 Java
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
113 0
java.lang.ClassNotFoundException: org.apache.commons.fileupload.disk.DiskFileItemFactory
您好,我是码农飞哥,感谢您阅读本文!本文主要介绍文件上传报的错
174 0
|
Apache
找不org.apache.commons.lang3.builder.EqualsBuilder和commons-lang下载链接
找不org.apache.commons.lang3.builder.EqualsBuilder和commons-lang下载链接
133 0
|
存储 Java Apache
java积累——apache commons fileupload 实现文件上传
java积累——apache commons fileupload 实现文件上传
396 0
|
Java Apache
org.apache.commons.fileupload.FileUploadBase$SizeL
上传94M的视频出现异常如下: [@APPNAME@] ERROR [http-80-3] MultiPartRequest.parse(130) | org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (102147245) exceeds the configured maximum (50097152) [@APPNAME@] ERROR [http-80-3] FileUploadInterceptor
389 0