FileUpload组件图片的上传

本文主要介绍如何在后端使用Apache Commons的FileUpload组件进行图片上传操作。首先需要引入commons-fileupload.jar和commons-io.jar库,然后创建表单并设置enctype为multipart/form-data。在后台,定义File、contentType和fileName变量,并检查文件类型和大小,将上传的图片保存到指定路径,并在上传成功后更新数据库。

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

上次谈了前端的技术,今天谈一下后端的吧!


今天来讲讲如何实现图片的上传和下载


关于这个的实现是有两种方法,一种是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,另外一种是通过strut2进行上传,这篇先来讲讲前者。


一、首先你要先下载好两个包,分别是commons-fileupload.jar和commons-io.jar包,将其导入到referenced libraries; 这里就不过多阐述了。


二、接下来现在jsp上面先写个表单用来提交数据

jsp:其中enctype="multipart/form-data" method="post"是关键,它是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作,这在原生的ajax里面应该挺熟悉的;<s:file name="upload"/>是s标签专门用来存放文件。

<s:form action="uploadPhoto?photo.userid=%{ #session.grsm.userid }"  enctype="multipart/form-data"  method ="post">       
    <s:textfield name="photo.photoName" label="文件名"/>
    <s:file name="upload"/>
    <s:submit value="上传"/>
    </s:form>


三、接下来看后台

action:

1、首先定义3个变量:

private File upload;
private String uploadContentType;  //文件类型  
private String uploadFileName;   //文件名

Java File类的功能非常强大,利用java基本上可以对文件进行所有操作。uploadContentType与uploadFileName是Commons的内置对象,分别设置他们的getter和setter方法,即可调用。

2、方法体:

public String uploadPhoto()throws IOException{

HttpServletResponse response = ServletActionContext.getResponse(); 

        
response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("utf-8");  
        PrintWriter out = response.getWriter(); 
        
        if(photoService.findRepeat(photo.getPhotoName())!=null){
        out.println("<script type=\"text/javascript\">");  
           out.println("alert('重复的图片名');");  
           out.println("</script>");  
           return null; 
        }else{

if(uploadContentType.equals("image/pjpeg") || uploadContentType.equals("image/jpeg")
||uploadContentType.equals("image/png")|| uploadContentType.equals("image/x-png")
||uploadContentType.equals("image/gif")||uploadContentType.equals("image/bmp")
||uploadContentType.equals("image/jpg")){

if (upload.length() > 1024 * 1024) {  
           out.println("<script type=\"text/javascript\">");  
           out.println("alert('不能大于1m');");  
           out.println("</script>");  
           return null;  
       }else{
        InputStream is = new FileInputStream(upload);
       
        String uploadPath = ServletActionContext.getServletContext().getRealPath("/img")+"/"+photo.getUserid();
        uploadContentType = uploadContentType.replace("/", ".");
        if(uploadContentType.equals("image.pjpeg") || uploadContentType.equals("image.jpeg")||uploadContentType.equals("image.jpg")){
        uploadContentType = ".jpg";

        }
        title = photo.getPhotoName()+uploadContentType;
        File file = new File(uploadPath); 
        if (!file.exists()) { // 如果路径不存在,创建  
           file.mkdirs();  
       }  
        File toFile = new File(uploadPath, title);
        OutputStream os = new FileOutputStream(toFile);  
       byte[] buffer = new byte[1024];  
       int length = 0;  
       while ((length = is.read(buffer)) > 0) {  
           os.write(buffer, 0, length);  

       }  
       is.close();  
       os.close();

       
       
       photo.setPhimg("img/"+photo.getUserid()+"/"+title);
       photoService.save(photo);
       
      out.println("<script type=\"text/javascript\">");  
           out.println("alert('上传成功');");  
           out.println("</script>");
       
       return null;
       
       }


}else{
out.println("<script type=\"text/javascript\">");  
           out.println("alert('必须是jpg或png或jpeg或gif或bmp的一种');");  
           out.println("</script>"); 
return null;
}
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值