JavaWeb学习笔记4——带进度条的文件上传,2024年最新面试举一个失败的经历范文

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

  • @throws ServletException if an error occurred

  • @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//禁止浏览器缓存

response.setHeader(“Cache-Control”, “no-store”);

response.setHeader(“Pragrma”, “no-cache”);

response.setDateHeader(“Expires”, 0);

UploadStatus status =(UploadStatus)request.getSession(true).getAttribute(“uploadStatus”); //从Session读取数据

if(status==null){

response.getWriter().println(“没有上传的信息”);

return;

}

//各种上传信息

long startTime=status.getStartTime();

long currentTime=System.currentTimeMillis();

long time =(currentTime-startTime)/1000+1;

double velocity =((double)status.getBytesRead())/(double)time;

double totalTime=status.getContentLength()/velocity;

double timeLeft=totalTime-time;

int percent=(int)(100*(double)status.getBytesRead()/(double)status.getContentLength());

double length =((double)status.getBytesRead())/1024/1024;

double totalLength=((double)status.getContentLength())/1024/1024;

String value =percent+“||”+length+“||”+totalLength+“||”+velocity+“||”

+time+“||”+totalTime+“||”+timeLeft+“||”+status.getItems();

response.getWriter().println(value); //输出给浏览器进度条

}

/**

  • The doPost method of the servlet.

  • This method is called when a form has its tag value method equals to post.

  • @param request the request send by the client to the server

  • @param response the response send by the server to the client

  • @throws ServletException if an error occurred

  • @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

UploadStatus status =new UploadStatus();

UploadListener listener =new UploadListener(status);

request.getSession(true).setAttribute(“uploadStatus”, status); //把状态放到Session里

ServletFileUpload upload =new ServletFileUpload(new DiskFileItemFactory()); //解析

upload.setProgressListener(listener);

try{

List itemList =upload.parseRequest(request); //提交的所有参数

for(Iterator it = itemList.iterator();it.hasNext()😉{

FileItem item =(FileItem) it.next();

if(item.isFormField()){ //如果是表单数据

System.out.println(“FormField:”+item.getFieldName()+“=”+item.getString());

}else { //否则是文件

System.out.println(“File:”+item.getName());

String fileName =item.getName().replace(“/”, “\”);

fileName =fileName.substring(fileName.lastIndexOf(“\”));

File saved =new File(“G:\upload_test”,fileName);

saved.getParentFile().mkdirs(); //保证路径存在

InputStream ins =item.getInputStream();

OutputStream ous =new FileOutputStream(saved);

//缓存写入

byte[] tmp =new byte[1024];

int len=-1;

while((len=ins.read(tmp))!=-1){

ous.write(tmp,0,len);

}

ous.close();

ins.close();

response.getWriter().println(“已保存文件:”+saved);

}

}

}catch(Exception e){

response.getWriter().println("上传发生错误: "+e.getMessage());

}

}

/**

  • Initialization of the servlet.

  • @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

显示上传进度

ProgressUpload.jsp的代码

<%@ page language=“java” contentType=“text/html; charset=UTF-8”%>

Insert title here




上传进度条:

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值