java web上传文件和下载文件代码片段分享
程序员文章站
2024-03-07 18:17:33
本文实例为大家分享了java web上传文件和下载文件的具体代码,供大家参考,具体内容如下
/**
* purpose:上传文档
*
* @...
本文实例为大家分享了java web上传文件和下载文件的具体代码,供大家参考,具体内容如下
/** * purpose:上传文档 * * @param req * @param filetitle * @param filetype * @param filedesc * @return */ @requestmapping("upload") public modelandview upload(httpservletrequest req, string filetype, string filedesc, string share) { userallinfo userall = (userallinfo) req.getsession().getattribute("userall"); modelandview mav = new modelandview(); // 获取文件的大小 string filesize = ""; int length = req.getcontentlength(); //转换成kb double len1 = (double) (math.round((length / 1024) * 100)) / 100; //转换成mb double len2 = (double) (math.round((len1 / 1024) * 100)) / 100; if (len2 > 1) { filesize = string.valueof(len2) + "mb"; } else { filesize = string.valueof(len1) + "kb"; } //将请求转换成处理文件的请求 multipartrequest mreq = (multipartrequest) req; //文件上传专用类 multipartfile mfile = mreq.getfile("uploadfile"); // 得到上下文路径 string root = req.getsession().getservletcontext().getrealpath("/"); file dir = new file(root); // 获取文件名和文件的mime类型 string str = mfile.getoriginalfilename(); string[] st = str.split("\\."); file savedfile = null; try { //在指定目录中创建一个新的空文件,文件名称加前缀"upload_" savedfile = file.createtempfile("upload_", mfile.getoriginalfilename(), dir); //将缓冲区的内容复制到新创建的文件中 filecopyutils.copy(mfile.getinputstream(), new fileoutputstream(savedfile)); } catch (exception e) { e.printstacktrace(); } string path = savedfile.getpath(); // 将文件的信息封装在对象中 fileinfo file = new fileinfo(); if (userall != null) { file.setuploadpersonid(userall.getuser().getuserid()); } else { mav.setviewname("login"); return mav; } file.setfilename(st[0]); file.setfilepath(path); date date = new date(); file.setuploaddate(date); file.setfilesize(filesize); file.setfiletype(filetype); file.setfileform(st[1]); if ("superman".equals(userall.getuser().getusernumber())) { file.setfileshare("y"); file.setcheckflag("y"); } else { file.setfileshare(share); file.setcheckflag("n"); } file.setdeleteflag("n"); file.setdownloadtimes(0); // 保存文件的信息 boolean bool = fileser.savefile(file); string usernumber = userall.getuser().getusernumber(); //设置跳转界面 mav.setviewname("jsp/person/upload"); logser.savelog("用户\"" + usernumber + "\"上传文件\"" + file.getfilename() + "\""); mav.getmodel().put("bool", bool); return mav; } /** * purpose:下载文档 * * @param fileid 文件id * @return */ @requestmapping("download") public void download(integer fileid, httpservletresponse resp) { fileinfo file = new fileinfo(); //通过文件id获取文件的基本信息 file = fileser.getfilebyid(fileid); // 根据文件后缀名设置文件的mime类型 string mime = "application/"; switch (file.getfileform()) { case "doc": mime = mime + "msword"; break; case "docx": mime = mime + "msword"; break; case "pdf": mime = mime + "pdf"; break; case "xls": mime = mime + "vnd.ms-excel"; case "ppt": mime = mime + "vnd.ms-powerpoint"; break; case "txt": mime = "text/plain"; break; default: break; } resp.setcontenttype(mime); string filename = file.getfilename(); string name = ""; try { // 将文件名使用utf-8编码格式编码,使其中的中文字符可以正常显示 name = urlencoder.encode(filename, "utf-8"); } catch (unsupportedencodingexception e1) { e1.printstacktrace(); } // 弹框选择保存路径 resp.setheader("content-disposition", "attachment;filename=" + name + "." + file.getfileform() + ";filename*=utf-8''" + name + "." + file.getfileform()); string filepath = file.getfilepath(); inputstream is = null; outputstream os = null; try { is = new bufferedinputstream(new fileinputstream(filepath)); os = new bufferedoutputstream(resp.getoutputstream()); byte[] buff = new byte[1024]; @suppresswarnings("unused") int count; while ((count = is.read(buff)) != -1) { os.write(buff); } //修改下载次数 fileser.update(fileid); } catch (exception e) { e.printstacktrace(); } finally { //最后,要记得关闭输入流和输出流 if (is != null) { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } if (os != null) { try { os.close(); } catch (ioexception e) { e.printstacktrace(); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。