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

Posted G-&-D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件的上传:二进制文件的上传;相关的知识,希望对你有一定的参考价值。

***二进制文件上传的方法:<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();
                        
                    //上传的文件的类型
                    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>

 

以上是关于文件的上传:二进制文件的上传;的主要内容,如果未能解决你的问题,请参考以下文章

HTML文件上传

Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”

jsp将图片等文件上传到服务器根目录下,读取二进制流存入mysql?怎么样实现?

将存储在内存中的文件上传到s3

android的自带的httpClient 怎么上传文件

上传到 iOS 应用商店时出现错误“您的二进制文件无效”