search
HomeJavaJavaBaseHow to upload files in java

How to upload files in java

Nov 20, 2019 pm 01:43 PM
javauploaddocument

How to upload files in java

1: Prepare the front page Upload.html

Form action=Upload file Backend interface method=”post” enctype=”multipart/form-data”, file Input box

<form action="/upload" method="post" enctype="multipart/form-data">
     <input type="file" name="files">
    <input type="submit" value="上传">
</form>

2: Add the corresponding jar package

How to upload files in java

##3: Prepare to receive the file Servlet

The url path must be consistent with the action in the form above

How to upload files in java

Four: Write the file upload background code

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
        try {
            // 配置上传参数
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            // 解析请求的内容提取文件数据
            @SuppressWarnings("unchecked")
            List<FileItem> formItems = upload.parseRequest(request);
            // 迭代表单数据
            for (FileItem item : formItems) {
                // 处理不在表单中的字段
                if (!item.isFormField()) {
                    String fileName = item.getName();
                    //定义上传文件的存放路径
                    String path = request.getServletContext().getRealPath("/uploadFiles");
                    //定义上传文件的完整路径
                    String filePath = String.format("%s/%s",path,fileName);
                    File storeFile = new File(filePath);
                    // 在控制台输出文件的上传路径
                    System.out.println(filePath);
                    // 保存文件到硬盘
                    item.write(storeFile);
                }
            }
        } catch (Exception ex) {
        }
    }

Five: Prepare for storage The directory of the uploaded file must be consistent with the path in the above code

How to upload files in java

Done!

Recommended tutorial:

java introductory tutorial

The above is the detailed content of How to upload files in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools