如何上传文件

  1. 在页面上面,form 表单里面添加属性enctype="multipart/form-data"

比如:

<form name="frm" method="post" enctype="multipart/form-data">

  1. 添加文件选择框:

<input type="file" name="uploadFile">

  1. 添加上传文件需要的包:

FileUpload – Download Apache Commons FileUpload

 

commons-fileupload-1.4.jar

以及依赖包:

commons-io-2.2.jar

  1. 编写代码:

在homepage.jsp页面:

<form name="frm" method="post" action="/user/uploadpicture" enctype="multipart/form-data">
    <input type="file" name="uploadFile"><br>
    <input type="submit" value="上传">
</form>

然后添加一个servlet处理上传文件

package com.hwadee.train.servlet;


import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;


@WebServlet("/user/uploadpicture")
public class UserUploadPictureServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("测试:UserUploadPictureServlet doPost");
        // 这个路径相对当前应用的目录
        String uploadPath = req.getServletContext().getRealPath("./")
                + File.separator + "upload"+ File.separator +"imgs";
        // 如果目录不存在则创建
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        // 配置上传参数
        DiskFileItemFactory factory = new DiskFileItemFactory();
//        // 设置临时存储目录
//        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            // 解析请求的内容提取文件数据
            @SuppressWarnings("unchecked")
            List<FileItem> formItems = upload.parseRequest(req);
            if (formItems != null && formItems.size() > 0) {
                // 迭代表单数据
                for (FileItem item : formItems) {
                    // 处理不在表单中的字段
                    if (!item.isFormField()) {
                        String fileName = new File(item.getName()).getName();
                        String filePath = uploadPath + File.separator + fileName;
                        File storeFile = new File(filePath);
                        // 在控制台输出文件的上传路径
                        System.out.println(filePath);
                        // 保存文件到硬盘
                        item.write(storeFile);
                    }
                }
            }
        } catch (Exception ex) {
            req.setAttribute("message",
                    "错误信息: " + ex.getMessage());
        }
        resp.sendRedirect("/homepage.jsp");
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

登登登__

期待每一份真诚

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值