文件的上傳:二進制文件的上傳;


***二進制文件上傳的方法:<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>

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com