文件上传 下载 解析 相对路径

有点坑吧,弄这么一个简单的东西弄了一天多,身边还有大神指导着,网上各种百度着。
下面总结一下遇到的问题:

文件上传,在页面上传的时候,不要想着去操作绝对路径,浏览器会对客户端的信息进行保护,避免用户信息收到攻击。
在上传图片,或者文件时,使用form表单来操作。
前台通过form表单传输一个流到后台,而不是ajax传递参数到后台,代码如下:

<form action="/tools/excel-upload.action" method="post"
enctype="multipart/form-data">
<!-- enctype="multipart/form-data" 文件上传使用 -->
<input type="file" name="fileupload" />
<input type="submit" value="UpLoad">
</form>


后台:

private File fileupload;


public static boolean copyFile(File oldPathFile, String newPathFile) throws IOException {

int bytesum = 0;
int byteread = 0;

if (oldPathFile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPathFile); // 读入原文件

FileOutputStream fs = new FileOutputStream(newPathFile);
byte[] buffer = new byte[FILEBYTE];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
fs.flush();
fs.close();
inStream.close();
return true;
}else{
return false;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值