基于java web的文件上传代码

  • d1_737809
    了解作者
  • 193.3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-10 12:43
    上传日期
基于java web的文件上传代码;基于java web的文件上传代码;基于java web的文件上传代码;
FileUploadSample.zip
内容介绍
import java.io.*; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.*; import org.apache.commons.fileupload.*; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.disk.*; public final class Upload extends HttpServlet { private final String SaveDirectory = new String("C:\\upload\\"); private final String TempDirectory = new String("C:\\temp\\"); private final int MaxMemorySize = 1048576; private final int MaxRequestSize = 5242880; private static final String CONTENT_TYPE = "text/html; charset=GBK"; public Upload() { super(); } private void existDirectory() { if (!new File(SaveDirectory).isDirectory()) new File(SaveDirectory).mkdirs(); if (!new File(TempDirectory).isDirectory()) new File(TempDirectory).mkdirs(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { request.setCharacterEncoding("GBK"); existDirectory(); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(MaxMemorySize); factory.setRepository(new File(TempDirectory)); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(MaxRequestSize); ProgressListener progressListener = new ProgressListener() { private long megaBytes = -1; public void update(long pBytesRead, long pContentLength, int pItems) { long mBytes = pBytesRead / 1000000; if (megaBytes == mBytes) { return; } megaBytes = mBytes; if (pContentLength == -1) { System.out.println("到目前, " + pBytesRead + " 字节的数据已经上传."); } else { System.out .println("到目前, " + pBytesRead + " / " + pContentLength + " 字节数据已经上传."); } } }; upload.setProgressListener(progressListener); List items = upload.parseRequest(request); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString(); System.out.println("表单域名: " + name+"\t" + "表单值: " + value ); } else { String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); System.out.println("文件域名: " + fieldName+"\t" + " 文件路径: " + fileName +"\t"+ " 上传类型:" +contentType+"\t"+"内存状态:"+isInMemory+"\t" +"文件大小(字节)"+sizeInBytes); File uploadedFile = new File(fileName); item.write(uploadedFile); } } response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.print("<script languaga=\"JavaScript\">alert(\"文件上传成功!\");window.location = \"index.html\";</script>"); } } catch (Exception e) { e.printStackTrace(); } } }
评论
    相关推荐
    • Java
      Java
    • java
      Java
    • Java
      Java
    • Java
      Java项目
    • Java
      Java 对于Java练习
    • java
      Java 此回购包括我学习Java的旅程。
    • java
      Java Java基础
    • Java
      Java
    • Java
      Java 我创建的Java项目
    • Java
      Java 2021/02/19 .java Main.java 学生.java Bmi.java 。班级 Bmi类 主类 MyBmi.class 学生班 学生样本类 2021/02/20 .java DeleteFile.java WriteFile.java MyDataPrint.java 。班级 DeleteFile.class ...