java 文件上传(jfinal框架)

1.jfinal 文件上传

1.2前端代码

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="UTF-8"%>
    <%String path = request.getContextPath();%>>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" charset="utf-8" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>File upload demo</h2>
<form action="<%=path%>/upload/upload1"  method="post" enctype="multipart/form-data">
  <input type="hidden" name="name" value="你好">
  <input type="file" name="filename" size="45"><br>
  <input type="submit" name="submit" value="submit">
  <a href="<%=path%>/down/downLoad?file=2019-12_瑞金市委党员考核统计表.xls">下载</a>
</form>
</body>
</html>

在使用jfinal的文件下载服务时候需要以下jar包:
cos-2019.8.jar 此包再jfinal的官方文件夹里有

package demo;

import java.io.File;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;

public class Upload extends Controller{
	/**
	 * FileUploadController 接收上传的文件
	 */
	
	    private final int MAXSize = 50 * 1024 * 1024; // 50M
	    private String filedir="E:\\";//指定用户训练视频文件上传路径(一般存放路径是默认的,如果需要随意路径的话需要在configConstant设置,设置如下`me.setBaseUploadPath("/");`)
	 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200302151842962.png)
	    /**
	     * 接收上传的文件
	     * 注意:前提是lib中引入cos-26Dec2008.jar包
	     */
	    public void upload1() {
	        // TODO Auto-generated method stub
	    
	        try {
//	          UploadFile upFile = getFile();//单个上传文件一句搞定  默认路径是 upload
//	          UploadFile upFile = getFile("FILE", filedir, maxSize, "utf-8");//只用于表单提交方式, 单个上传文件
	            List<UploadFile> upFiles = getFiles(filedir, MAXSize, "utf-8");//批量上传文件
	            String name =getPara("name");
	            System.out.println(name);
	            for (UploadFile fileItem : upFiles) {
	            	File file =new File("E:\\upload\\"+fileItem.getOriginalFileName());
	            	//判断文件夹是否存在
	            	if (!file.getParentFile().exists()) {
	            	file.getParentFile().mkdirs();// 创建文件夹
	            	}
	            	// file文件存在则删除并执行上传,不存在则直接执行上传
	            	if (file.exists()) {
	            	// 删除旧文件
	            	file.delete();
	            	}
	            	fileItem.getFile().renameTo(file);// 上传文件到指定位置
	            	//获取上传文件大小
	            	long size = fileItem.getFile().length();
	            	System.out.println(size);
	                String fPath=filedir+fileItem.getOriginalFileName();//获取上传文件名
	                System.out.println("上传fPath"+fPath);
	                String newPath=filedir+fileItem.getOriginalFileName().replace(".", "1.");//例如:交叉接触动作1.3gp
	                File oldFile=new File(fPath);
	                File newFile=new File(newPath);
	                if(newFile.exists()){
	                    System.out.println("-------------删除"+fileItem.getOriginalFileName());
	                    //删除旧的
	                    oldFile.delete();
	                    //新的重命名
	                    boolean updateName=newFile.renameTo(oldFile);
	                    System.out.println("-------------updateName:"+updateName);
	                     
	                }
	                 
	            }
	             
	        } catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	            renderJson("status", "0");//失败
	        }
	        renderJson("status", "1");//成功
	 
	   
	}
}

2.文件下载

(一般存放路径是默认的,如果需要随意路径的话需要在configConstant设置,设置如下me.setBaseUploadPath("/");
/**
* Set file base download path for Controller.renderFile(…)
* 设置文件下载基础路径,当路径以 “/” 打头或是以 windows 磁盘盘符打头,
* 则将路径设置为绝对路径,否则路径将是以应用根路径为基础的相对路径
*


* 例如:
* 1:参数 “/var/www/download” 为绝对路径,下载文件存放在此路径之下
* 2:参数 “download” 为相对路径,下载文件存放在 PathKit.getWebRoot() + “/download” 路径之下
*

*/
在这里插入图片描述

package demo;

import java.io.File;

import com.jfinal.core.Controller;

public class Download extends Controller{

	
	public void downLoad() {
		
		String filename = getPara("file");//
		String path = "E:\\upload\\";
		File file = new File(path + "/"+filename);
		System.out.println(path);
		System.out.println(file.exists());//测试此抽象路径名表示的文件或目录是否存在
		if (file.exists()) {
		renderFile(file);
		} else {
		renderJson("文件不存在");
		}
		
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值