1 springboot项目中将文件存放项目中的resouces目录下访问
// 获取授权文件路径
String path = Thread.currentThread().getContextClassLoader()
.getResource("文件名").getPath();
2 springboot中将不断生成的项目文件存放在linux/windowns中的tomcat目录中;(最好存放在文件服务器中)
long currentTimeMillis = System.currentTimeMillis();
String path = System.getProperty("catalina.home");// /usr/local/tomcat
File mdirfile = new File(path + resoucepath);
if (!mdirfile.exists()) {// 如果文件夹不存在
mdirfile.mkdir();// 创建文件夹
}
String pripath = path + resoucepath + "/" + "文件名称"+ "_"+ currentTimeMillis;
File mfile = new File(pripath );
file.transferTo(mfile);//其中file是MultipartFile类型
public void writeToFile(String filePath, String content) {
try {// 流的套接
FileOutputStream fout = new FileOutputStream(filePath);
DataOutputStream dout = new DataOutputStream(fout);
try {
File writeName = new File(filePath); // 相对路径,如果没有则要建立一个新的output.txt文件
writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
try (FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer)) {
out.write(content);
out.flush(); // 把缓存区内容压入文件
}
} catch (IOException e) {
e.printStackTrace();
}
fout.close();
dout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object readFromFile(String fileName) throws Exception {
String resultCode = "";
File file = new File(fileName);
// InputStream in = null;
if (file.isFile() && file.exists()) { // 判断文件是否存在
FileInputStream fis = new FileInputStream(file);
// Construct BufferedReader from InputStreamReader
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
resultCode += line;
}
br.close();
fis.close();
} else {
Slf4jLogUtil.info("找不到指定的文件,请确认文件路径是否正确");
return null;
}
return resultCode;
}
3 下载文件
DownloadUtil du = new DownloadUtil();
du.download(“文件路径”, “返回的文件名称”, response, “是否删除文件”);
package com.people.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.ranges.RangeException;
import com.people.common.exception.RRException;
import com.people.exception.RRExceptionHandler;
public class DownloadUtil {
/**
* @param filePath 要下载的文件路径
* @param returnName 返回的文件名
* @param response HttpServletResponse
* @param delFlag 是否删除文件
*/
public void download(String filePath,String returnName,HttpServletResponse response,boolean delFlag) throws Exception{
this.prototypeDownload(new File(filePath), returnName, response, delFlag);
}
/**
* @param file 要下载的文件
* @param returnName 返回的文件名
* @param response HttpServletResponse
* @param delFlag 是否删除文件
*/
protected void download(File file,String returnName,HttpServletResponse response,boolean delFlag) throws Exception{
this.prototypeDownload(file, returnName, response, delFlag);
}
/**
* @param file 要下载的文件
* @param returnName 返回的文件名
* @param response HttpServletResponse
* @param delFlag 是否删除文件
*/
public void prototypeDownload(File file,String returnName,HttpServletResponse response,boolean delFlag) throws Exception{
// 下载文件
FileInputStream inputStream = null;
ServletOutputStream outputStream = null;
if(!file.exists()){
throw new RRException("文件不存在!");
}
try {
response.reset();
//设置响应类型 PDF文件为"application/pdf",WORD文件为:"application/msword", EXCEL文件为:"application/vnd.ms-excel"。
response.setContentType("application/octet-stream;charset=utf-8");
//设置响应的文件名称,并转换成中文编码
//returnName = URLEncoder.encode(returnName,"UTF-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); //保存的文件名,必须和页面编码一致,否则乱码
//attachment作为附件下载;inline客户端机器有安装匹配程序,则直接打开;注意改变配置,清除缓存,否则可能不能看到效果
response.addHeader("Content-Disposition", "attachment;filename="+returnName);
//将文件读入响应流
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
int length = 1024;
int readLength=0;
byte buf[] = new byte[1024];
readLength = inputStream.read(buf, 0, length);
while (readLength != -1) {
outputStream.write(buf, 0, readLength);
readLength = inputStream.read(buf, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//删除原文件
if(delFlag) {
file.delete();
}
}
}
/**
* by tony 2013-10-17
* @param byteArrayOutputStream 将文件内容写入ByteArrayOutputStream
* @param response HttpServletResponse 写入response
* @param returnName 返回的文件名
*/
public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException{
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); //保存的文件名,必须和页面编码一致,否则乱码
response.addHeader("Content-Disposition", "attachment;filename=" + returnName);
response.setContentLength(byteArrayOutputStream.size());
ServletOutputStream outputstream = response.getOutputStream(); //取得输出流
byteArrayOutputStream.writeTo(outputstream); //写到输出流
byteArrayOutputStream.close(); //关闭
outputstream.flush(); //刷数据
}
}
package com.people.util;
import java.io.File;
import com.people.common.utils.Slf4jLogUtil;
public class FileUtis {
/**
* 判断指定的文件或文件夹删除是否成功
*
* @param FileName
* 文件或文件夹的路径
* @return true or false 成功返回true,失败返回false
*/
public static boolean deleteAnyone(String FileName) {
File file = new File(FileName);// 根据指定的文件名创建File对象
if (!file.exists()) { // 要删除的文件不存在
System.out.println("文件" + FileName + "不存在,删除失败!");
Slf4jLogUtil.error("文件" + FileName + "不存在,删除失败!");
return false;
} else { // 要删除的文件存在
if (file.isFile()) { // 如果目标文件是文件
return deleteFile(FileName);
} else { // 如果目标文件是目录
return deleteDir(FileName);
}
}
}
/**
* 判断指定的文件删除是否成功
*
* @param FileName
* 文件路径
* @return true or false 成功返回true,失败返回false
*/
public static boolean deleteFile(String fileName) {
File file = new File(fileName);// 根据指定的文件名创建File对象
if (file.exists() && file.isFile()) { // 要删除的文件存在且是文件
if (file.delete()) {
System.out.println("文件" + fileName + "删除成功!");
Slf4jLogUtil.info("文件" + fileName + "删除成功!");
return true;
} else {
System.out.println("文件" + fileName + "删除失败!");
Slf4jLogUtil.error("文件" + fileName + "删除失败!");
return false;
}
} else {
System.out.println("文件" + fileName + "不存在,删除失败!");
Slf4jLogUtil.error("文件" + fileName + "不存在,删除失败!");
return false;
}
}
/**
* 删除指定的目录以及目录下的所有子文件
*
* @param dirName
* is 目录路径
* @return true or false 成功返回true,失败返回false
*/
public static boolean deleteDir(String dirName) {
if (dirName.endsWith(File.separator))// dirName不以分隔符结尾则自动添加分隔符
dirName = dirName + File.separator;
File file = new File(dirName);// 根据指定的文件名创建File对象
if (!file.exists() || (!file.isDirectory())) { // 目录不存在或者
System.out.println("目录删除失败" + dirName + "目录不存在!");
Slf4jLogUtil.error("目录删除失败" + dirName + "目录不存在!");
return false;
}
File[] fileArrays = file.listFiles();// 列出源文件下所有文件,包括子目录
for (int i = 0; i < fileArrays.length; i++) {// 将源文件下的所有文件逐个删除
FileUtis.deleteAnyone(fileArrays[i].getAbsolutePath());
}
if (file.delete())// 删除当前目录
System.out.println("目录" + dirName + "删除成功!");
Slf4jLogUtil.info("目录" + dirName + "删除成功!");
return true;
}
/** 删除指定目录下的文件(包括目录) **/
public static void main(String[] args) {
// 创建目标文件路径
String fileName = "C://Users//zhaozhiqiang//Desktop//yinni-api.war";
boolean deleteAnyone = FileUtis.deleteAnyone(fileName);// 调用删除目标文件方法
}
}
4返回属性时候注解
@JsonIgnore //不管是将 java 对象转换成 json 字符串,还是将 json 字符串转换成 java 对象。都会忽略
private Timestamp createTime;
@JsonIgnoreProperties 和 @JsonIgnore 的作用相同,都是告诉 Jackson 该忽略哪些属性,
不同之处是 @JsonIgnoreProperties 是类级别的,并且可以同时指定多个属性。
@JsonIgnoreProperties(value = {"createTime","updateTime"})
public class SellerInfoEntity {
private Timestamp createTime;
private Timestamp updateTime;
}
只在序列化情况下生效的注解
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String password;
5 mybatis注解sql开发
public interface GoodsDao extends BaseMapper {
//降序
@Select({""})
List selectListDESC(GoodsEntity goodsEntity);
//升序
@Select({""})
List selectListASC(GoodsEntity goodsEntity);
//默認
@Select({""})
List selectList(GoodsEntity goodsEntity);
//",
"select * from auth_log where user_id = #{userId} and token_id = #{tokenId} "
+
" and account_id = #{accountId} "
+ "and auth_passwd = #{authPwd} and date(created_at) = curdate() "
+ "and deleted_at is null limit 10", "" })
public List getAuthLogList(QueryAuthLogVo queryVo);
}
/**
* 单参数List 类型
* @param list2
*/
@Update({
"" })
void updateTypeAndTimeList(List list2);
/**
* 单参数array 类型
* @param array
*/
@Update({
"" })
void updateTypeAndTimearray(String[] array);
/**
* 自己把参数封装成Map的类型
* @param Map
*/
@Update({
"" })
void updateTypeAndTimeMap(Map map);
6 mybatis中的mapper xml开发
id, ad_code, pic_url, pic_link_url, pic_sort, is_available,
is_deleted,
create_user,
create_time, update_user, update_time
and is_deleted=0
and is_available=1
and ad_code=#{adCode,jdbcType=VARCHAR}
and id in
#{id,jdbcType=BIGINT}
id, dic_key, dic_pkey, dic_value, dic_comment
and dicKey in
#{dicKey,jdbcType=INTEGER}
and dic_key = #{dicKey,jdbcType=INTEGER}
and dic_pkey = #{dicPkey,jdbcType=INTEGER}
cao.id, cao.order_code, cao.user_name, cao.user_phone,
cao.order_source,
cao.activity_name, cao.activity_time,
cao.activity_place,
cao.create_time, cao.total_amount, cao.pay_way,
cao.status,
cao.is_entry,cao.update_time,
cao.is_deleted,
cao.is_available,
cao.create_user_id, cao.update_user, cao.bar_num,
cao.bar_area,
cao.refund_state,
cao.isPlus, cao.out_state, cao.out_code,
cao.main_type,
cao.pay_state, cao.pay_time,
cao.reserve_name,
cao.reserve_phone,
cao.community_activity_id,cao.surface_picture,cao.qr_url,cao.is_admin,cao.is_sign,cao.sign_time
and cao.is_deleted=0
and cao.community_activity_id =
#{communityActivityId,jdbcType=BIGINT}
and cao.is_deleted=0
and cao.id in
#{id,jdbcType=BIGINT}
and cao.order_code = #{orderCode,jdbcType=VARCHAR}
and cao.order_code in
#{orderCode,jdbcType=VARCHAR}
ob.id, ob.code, ob.name, ob.zone_id, ob.province_name, ob.city_name, ob.region_name,
ob.province_code,
ob.city_code,
ob.region_code, ob.address_detail, ob.contact_tel,
ob.building_area, ob.rent_style, ob.sale_state, ob.business_start_time,
ob.business_end_time,
ob.description,
ob.home_url, ob.url, ob.qr_ext, ob.is_available, ob.is_deleted, ob.create_user, ob.create_time,
ob.update_user,
ob.update_time, ob.lng, ob.lat
and ob.zone_id = z.id
and ob.is_deleted=0
and z.is_deleted=0
and ob.is_available=#{isAvailable,jdbcType=INTEGER}
and ob.id = #{id,jdbcType=BIGINT}
and z.user_id = #{userId,jdbcType=BIGINT}
and ob.name like CONCAT('%',#{name,jdbcType=VARCHAR},'%')
and ob.zone_id = #{zoneId,jdbcType=BIGINT}
and ob.id in
#{id,jdbcType=BIGINT}
OI.id, OI.order_code, OI.item_id, OI.item_code, OI.item_name, OI.item_classify, OI.item_type, OI.item_date,
OI.item_starttime, OI.item_endtime, OI.item_hours, OI.item_amount, OI.item_remark, OI.item_price, OI.item_num,
OI.item_phone, OI.item_total_price, OI.item_about, OI.item_content, OI.item_ask_code, OI.is_available,
OI.is_deleted, OI.create_user, OI.create_time, OI.update_user, OI.update_time, OI.discount_amount,
OI.item_discount
OM.id order_id,
OM.order_date,
OM.order_starttime,
OM.order_endtime,
OM.order_hours,
OM.order_amount,
OM.total_amount,
OM.payway,
OM.account_pay_mode,
OM.user_id,
OM.mobile,
OM.remark,
OM.is_invoiced,
OM.invoice_id,
OM.status,
OM.pay_state,
OM.sources,
OM.is_available,
OM.is_deleted,
OM.create_user,
OM.create_time,
OM.update_user,
OM.update_time,
OM.main_type,
OM.order_type,
OM.work_building_id,
OM.office_name,
OM.province_name,
OM.city_name,
OM.region_name,
OM.address_detail,
OM.business_start_time,
OM.business_end_time,
OM.product_id,
OM.product_name,
OM.product_type,
OM.line_map_url,
OM.restaurant_id,
OM.identity_card_name,
OM.qualifications,
OM.industry_first,
OM.industry_second,
OM.head_pic_url,
OM.company_name,
OM.date_code,
OM.reservation_num,
OM.is_blind,
OM.discount_amount,
OM.is_display_reservation,
OM.out_code,
OM.out_state,
OM.refund_state,
OM.meeting_title,
OM.dispatch_time,
OM.dispatch_zone_id
U.company_name user_company_name,
U.identity_card_name user_identity_card_name,
U.head_pic_url user_head_pic_url
I.industry_name item_about_desc
,
,
,
and OI.is_deleted=0
and OI.id in
#{id,jdbcType=BIGINT}
and OI.order_code = #{orderCode,jdbcType=VARCHAR}
and OI.order_code in
#{orderCode,jdbcType=VARCHAR}
and OI.item_code = #{itemCode,jdbcType=VARCHAR}
and OI.item_id = #{itemId,jdbcType=BIGINT}
and OI.item_type = #{itemType,jdbcType=INTEGER}
and OI.item_classify = #{itemClassify,jdbcType=INTEGER}
and OI.item_classify in
#{itemClassify,jdbcType=INTEGER}
and OM.is_deleted=0
and OM.order_date = #{orderDate,jdbcType=DATE}
and OM.mobile = #{mobile,jdbcType=VARCHAR}
and OM.status = #{status,jdbcType=INTEGER}
and OM.order_type = #{orderType,jdbcType=INTEGER}
and OM.user_id = #{userId,jdbcType=BIGINT}
and DATE_FORMAT(OM.order_date,'%Y-%m-%d') =]]> DATE_FORMAT(#{orderStartDate,jdbcType=DATE},'%Y-%m-%d')
and DATE_FORMAT(OM.order_date,'%Y-%m-%d') DATE_FORMAT(#{orderEndDate,jdbcType=DATE},'%Y-%m-%d')
and DATE_FORMAT(OM.order_starttime,'%H:%i:%s') =]]> DATE_FORMAT(#{orderStartTime,jdbcType=DATE},'%H:%i:%s')
and DATE_FORMAT(OM.order_endtime,'%H:%i:%s') DATE_FORMAT(#{orderEndTime,jdbcType=DATE},'%H:%i:%s')
and DATE_FORMAT(OM.create_time,'%Y') =#{year,jdbcType=VARCHAR}
and OM.is_display_reservation = #{isDisplayReservation,jdbcType=BIT}
and OM.status in
#{status,jdbcType=INTEGER}
id, order_code, order_item_id, person_id, person_name, person_mobile, arrive_time,
is_arrived, person_type, remark, is_available, is_deleted, create_user,
create_time, update_user, update_time
and is_deleted=0
and id in
#{id,jdbcType=BIGINT}
and order_code = #{orderCode,jdbcType=VARCHAR}
and person_name like CONCAT('%', #{personName,jdbcType=VARCHAR}, '%')
and person_mobile = #{personMobile,jdbcType=VARCHAR}
and person_type = #{personType,jdbcType=VARCHAR}
OJP.id,
OJP.order_code,
OJP.order_item_id,
OJP.person_name,
OJP.person_mobile,
OJP.arrive_time,
OJP.is_arrived,
OJP.remark,
OJP.is_available,
OJP.is_deleted,
OJP.create_user,
OJP.create_time,
OJP.update_user,
OJP.update_time,
OM.product_name,
OM.order_date,
OM.order_starttime,
OM.order_endtime,
OM.mobile,
U.identity_card_name
and OJP.is_deleted=0
and OJP.id in
#{id,jdbcType=BIGINT}
and OJP.order_code = #{orderCode,jdbcType=VARCHAR}
and OJP.person_name like CONCAT('%', #{personName,jdbcType=VARCHAR}, '%')
and OJP.person_mobile = #{personMobile,jdbcType=VARCHAR}
and OJP.person_type = #{personType,jdbcType=VARCHAR}
and OM.order_date = #{orderDate,jdbcType=DATE}
and OM.status = #{orderStatus,jdbcType=INTEGER}
and OM.status in
#{status,jdbcType=INTEGER}