Java文件上传

本文详细介绍了如何使用Ajax进行文件上传操作,并展示了如何在后端处理这些上传的文件,包括文件大小检查、重命名、保存及返回上传状态。同时,文章提供了完整的代码示例,涵盖前端Ajax调用和后端文件处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%-- 上传文件的 jsp --%>

上传

/* 用ajax提交 /
KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲uploadBtn").cli…("#form")[0])
KaTeX parse error: Expected '}', got 'EOF' at end of input: …, url:"{pageContext.request.contextPath}/witkeyDemandScheme/addwitkeyDemandScheme",
data:fd
dataType:“json”,
processData:false,
contentType:false,
success:function (rs) {
var str=$("#showFiles").html();
if(rs.length>0){
for(var i=0;i<rs.length;i++){
/
拼接提交后的文件名 回显到页面 */
str+="

<span id='sp’style=‘font-size: 8px’>"+rs[i]+"<button id='deleteupload’‘shanchu’("+rs[i]+")>删除

"
}
}
$("#showFiles").html(str);
},
error:function (err) {
console.log(err);
}
})
});

/*Connection层 */
@ResponseBody
@RequestMapping("/addwitkeyDemandScheme")
public List addwitkeyDemandScheme(@RequestParam(“files”)MultipartFile[] files, String demandid, HttpSession session){
List list=new ArrayList();
WitkeyUser user = (WitkeyUser) session.getAttribute(“user”);
if(usernull){
return list;
}
if(files
null||files.length==0){
return list;
}
String realPath = session.getServletContext().getRealPath("/uploads/scheme");

for(int i=0;i<files.length;i++){
    long size =files[i].getSize();
    if(size>1000000){
        list.add("该文件太大,无法上传");
        return  list;
    }
    String fileName=files[i].getOriginalFilename();
    fileName= saveNewFileName(fileName,demandid,user.getId());
    list.add(fileName);
    File f=new File(realPath+File.separator+fileName);

    try {
        files[i].transferTo(f);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
int i = witkeyDemandSchemeService.addwitkeyDemandScheme(demandid, user.getId(), list);
logger.debug("返回的数为:"+i);
return list;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Angzush

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值