JFinal文件上传、下载、删除、修改文件名
2023-06-05 15:53:36来源:个人图书馆-hncdman

Tz.

于 2019-03-29 16:48:47 发布

756


(相关资料图)

收藏 2

分类专栏: java

版权

java

专栏收录该内容

21 篇文章0 订阅

订阅专栏

1.文件上传:

ObjectVo objectVo=new ObjectVo();

try {

UploadFile file=getFile();

String cou_number="201811";

String session= String.valueOf(getSession());

//大小限制

System.out.println(file.getFile().length()+" "+maxPostSize);

if (file.getFile().length()>maxPostSize){

objectVo.setCode(0);

objectVo.setMsg("超出大小限制");

renderJson(objectVo);

}

//默认路径+文件名

String path=file.getUploadPath()+"/"+file.getFileName();

//文件大小

double data_size=file.getFile().length();

String tea_upload=Db.getSql("teacher.teacherUploadData");

String filename=file.getFileName();

//文件名

String name=filename.substring(0,filename.lastIndexOf("."));

//文件类型

String data_type=(filename.substring(file.getFileName().lastIndexOf("."))).replace(".","");

Integer flag=Db.update(tea_upload,name,data_type,data_size,session,cou_number,path);

objectVo.setCode(flag);

objectVo.setMsg("文件上传成功!");

}catch (Exception e){

e.printStackTrace();

objectVo.setMsg("文件上传失败!");

objectVo.setCode(0);

}

renderJson(objectVo);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

2.文件下载:

String name=文件名;

String type="docx";

try {

String path = getSession().getServletContext().getRealPath("/static/modules/upload");

path=path+"\\"+name+"."+type;

System.out.println(path);

File file=new File(path);

renderFile(file);

}catch (Exception e){

renderJson("文件下载失败!");

}

1

2

3

4

5

6

7

8

9

10

11

3. 修改文件名称

String id="30";

String oriname="t";

String data_type="txt";

String newname="t1";

ObjectVo objectVo=new ObjectVo();

try {

String path = getSession().getServletContext().getRealPath("/static/modules/upload");

String newpath=path+"\\"+oriname+"."+data_type;

File file=new File(newpath);

if (file.exists()){

file.renameTo(new File(path+"\\"+newname+"."+data_type));

objectVo.setCode(1);

objectVo.setMsg("文件修改成功!");

}else {

objectVo.setCode(0);

objectVo.setMsg("文件不存在!");

}

}catch (Exception e){

objectVo.setCode(0);

objectVo.setMsg("文件修改失败!");

}

renderJson(objectVo);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

4.文件删除

String id="1";

String oriname="tz";

String data_type="txt";

ObjectVo objectVo=new ObjectVo();

String path = getSession().getServletContext().getRealPath("/static/modules/upload");

String newpath=path+"\\"+oriname+"."+data_type;

try {

File file=new File(newpath);

if (file.exists()){

file.delete();

objectVo.setCode(1);

objectVo.setMsg("文件删除成功!");

}else {

objectVo.setCode(0);

objectVo.setMsg("文件删除失败,请检查文件是否存在!");

}

}catch (Exception e){

objectVo.setMsg("文件删除失败!");

objectVo.setCode(0);

}

renderJson(objectVo);

————————————————

版权声明:本文为CSDN博主「Tz.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_42551369/article/details/88896865

标签:

下一篇: 最后一页
上一篇: 新加坡防长:日本得做点啥,让中国放心

相关新闻

保险时讯