文件的上传:二进制文件的上传;

时间:2022-01-12 20:55:55

***二进制文件上传的方法:<form action="uploadServlet" method="post" enctype="multipart/form-data">

***在lib目录下:导入文件上传的开源架包:commons-fileupload-1.2.1.jar,commons-io-2.0.jar;

 

建立Servlet类:UploadServlet,实现文件上传的方法package com.lanqiao.javaweb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// 1. 得到 FileItem 的集合 items
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();

// FileCleaningTracker fileCleaningTracker =
// FileCleanerCleanup.getFileCleaningTracker(getServletContext());
// factory.setFileCleaningTracker(fileCleaningTracker);

// Set factory constraints,设置文件的大小
factory.setSizeThreshold(1024 * 500);

//超过该大小,就读到临时的文件里面
File tempDirectory = new File("f:\\tempDirectory");
factory.setRepository(tempDirectory);

// Create a new file upload handler;上传该文件
ServletFileUpload upload = new ServletFileUpload(factory);

// Set overall request size constraint;设置文件总的大小
upload.setSizeMax(1024 * 1024 * 5);

// Parse the request
try {
//获取jsp文件里的请求,在集合中
List<FileItem> /* FileItem */items = upload.parseRequest(request);

// 2. 遍历 items:
for (FileItem item : items) {
// 若是一个一般的表单域, 打印信息
if (item.isFormField()) {
String name
= item.getFieldName();
String value
= item.getString();

System.out.println(name
+ ": " + value);


}
// 若是文件域则把文件保存到 d:\\files 目录下.
else {
//文件名为file
String fieldName = item.getFieldName();

//上传的原始的文件名
String fileName = item.getName();

       

              //截取路径名,因为想要获取的是文件名而不是文件的路径
              fileName=fileName.substring(3);

//上传的文件的类型
String contentType = item.getContentType();

//上传的文件的大小
long sizeInBytes = item.getSize();
System.out.println(fieldName);
System.out.println(fileName);
System.out.println(contentType);
System.out.println(sizeInBytes);

InputStream in
= item.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;

//文件上传后的 路径和文件名
fileName = "f:\\files\\" + fileName;
System.out.println(fileName);

OutputStream out
= new FileOutputStream(fileName);

while ((len = in.read(buffer)) != -1) {
out.write(buffer,
0, len);
}

out.close();
in.close();
}
}

}
catch (FileUploadException e) {
e.printStackTrace();
}

}

}

 

web.xml文件配置和映射:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>day-14-fileupload</display-name>
<servlet>
<description></description>
<display-name>UploadServlet</display-name>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.lanqiao.javaweb.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/uploadServlet</url-pattern>
</servlet-mapping>
</web-app>

 

upload.jsp表单:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding
="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="uploadServlet" method="post" enctype="multipart/form-data">

&nbsp;&nbsp;File:&nbsp;&nbsp;<input type="file" name="file"/>
<br><br>
Desc:
&nbsp;&nbsp;<input type="text" name="desc"/>
<br><br>
<input type="submit" value="Submit"/>
</form>

</body>
</html>