• 大小: 9.88M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-08
  • 语言: 其他
  • 标签: 其他  

资源简介

源码.zip

资源截图

代码片段和文件信息

package com.ps;

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

import javax.servlet.ServletException;
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(name = “ceshi1“)
public class PhotoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {


        request.setCharacterEncoding(“utf-8“);  //设置编码
        //获得磁盘文件条目工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //获取文件需要上传到的路径
        String path = request.getRealPath(“/upload“);
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        factory.setRepository(new File(path));
        //设置 缓存的大小
        factory.setSizeThreshold(1024*1024) ;
        //文件上传处理
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            //可以上传多个文件
            List list = (List)upload.parseRequest(request);
            for(FileItem item : list){
                //获取属性名字
                String name = item.getFieldName();
                //如果获取的 表单信息是普通的 文本 信息
                if(item.isFormField()){
                    //获取用户具体输入的字符串因为表单提交过来的是 字符串类型的
                    String value = item.getString() ;
                    request.setAttribute(name value);
                }else{
                    //获取路径名
                    String value = item.getName() ;
                    //索引到最后一个反斜杠
                    int start = value.lastIndexOf(“\\“);
                    //截取 上传文件的 字符串名字,加1是 去掉反斜杠,
                    String filename = value.substring(start+1);
                    request.setAttribute(name filename);
                    //写到磁盘上
                    item.write( new File(pathfilename) );//第三方提供的
                    System.out.println(“上传成功:“+filename);
                    response.getWriter().print(filename);//将路径返回给客户端
                }
            }

        } catch (Exception e) {
            System.out.println(“上传失败“);
            response.getWriter().print(“上传失败:“+e.getMessage());
        }
    }

    protected void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
        doPost(requestresponse);
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-10-15 19:52  婧愮爜\
     文件        6148  2017-10-15 19:49  婧愮爜\.DS_Store
     目录           0  2017-10-16 08:27  __MACOSX\
     目录           0  2017-10-16 08:27  __MACOSX\婧愮爜\
     文件         120  2017-10-15 19:49  __MACOSX\婧愮爜\._.DS_Store
     目录           0  2017-10-15 10:46  婧愮爜\ceshi1\
     目录           0  2017-10-15 12:18  婧愮爜\ceshi1\.idea\
     目录           0  2017-10-07 19:48  婧愮爜\ceshi1\.idea\artifacts\
     文件         487  2017-10-07 19:48  婧愮爜\ceshi1\.idea\artifacts\ceshi1_war_exploded.xml
     文件         686  2017-10-07 19:48  婧愮爜\ceshi1\.idea\compiler.xml
     目录           0  2017-10-07 19:48  婧愮爜\ceshi1\.idea\copyright\
     文件          74  2017-10-07 19:48  婧愮爜\ceshi1\.idea\copyright\profiles_settings.xml
     目录           0  2017-10-15 10:46  婧愮爜\ceshi1\.idea\libraries\
     文件         328  2017-10-15 10:46  婧愮爜\ceshi1\.idea\libraries\commons_fileupload_1_3_1.xml
     文件        3811  2017-10-15 10:06  婧愮爜\ceshi1\.idea\misc.xml
     文件         252  2017-10-07 19:48  婧愮爜\ceshi1\.idea\modules.xml
     文件       45538  2017-10-15 12:18  婧愮爜\ceshi1\.idea\workspace.xml
     文件         997  2017-10-15 10:46  婧愮爜\ceshi1\ceshi1.iml
     目录           0  2017-10-15 10:57  婧愮爜\ceshi1\out\
     目录           0  2017-10-07 19:49  婧愮爜\ceshi1\out\artifacts\
     目录           0  2017-10-15 11:36  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\
     文件         195  2017-10-15 19:52  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\index.jsp
     目录           0  2017-10-15 12:13  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\upload\
     文件       11995  2017-10-15 12:13  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\upload\dts_featured_quiet_comfort_qc.png
     文件      459187  2017-10-15 12:08  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\upload\Picture_01_Shark.jpg
     文件      729422  2017-10-15 11:52  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\upload\Picture_04_Lake.jpg
     文件        3729  2017-10-15 11:53  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\upload\screen_lock_27149.png
     目录           0  2017-10-15 11:30  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\WEB-INF\
     目录           0  2017-10-15 10:58  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\WEB-INF\classes\
     目录           0  2017-10-15 10:58  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\WEB-INF\classes\com\
     目录           0  2017-10-15 11:49  婧愮爜\ceshi1\out\artifacts\ceshi1_war_exploded\WEB-INF\classes\com\ps\
............此处省略1906个文件信息

评论

共有 条评论