1.3K Star 7.3K Fork 4K

JEECG开源社区 / jeecg-boot

 / 详情

附件下载的文件报400

Done
Opened this issue  
2021-04-23 09:43
版本号:

v2.4.3

问题描述:

1.带有【xx.x“xxx”xxxx.docx】的文件无法正常下载下来,会直接报出400,
2.文件上传后文件名带有.的将会出现错误的名称
附件2.zzz.xxx.docx
上传后
fujian2.zzz.xxx_1619140002776.zzz.xxx.docx

问题截图:

输入图片说明

解决方法:

1.【org.jeecg.modules.system.controller.CommonController】.uploadLocal

String orgName = mf.getOriginalFilename();// 获取文件名
orgName = CommonUtils.getFileName(orgName);
orgName = orgName.replace("“","").replace("”","");//去除“和”,不会导致上传错误,但会产生下载错误(现在发现很多需要替换掉字符)
if(orgName.indexOf(".")!=-1){
	fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
}else{
	fileName = orgName+ "_" + System.currentTimeMillis();
}

友情提示: 未按格式要求发帖,会直接删掉。

Comments (3)

柠檬不萌 created任务
展开全部操作日志
柠檬不萌 changed description

上传时文件名不符合url要求(引起400的那种)替换成空串
现解决方案

//orgName = orgName.replace("“","").replace("”","");//去除“和”,不会导致上传错误,但会产生下载错误
orgName = com.qnsoft.tools.URLEncoder.DEFAULT.encode(orgName);

新增URLEncoder.java

package com.qnsoft.tools;

import cn.hutool.core.util.CharsetUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.BitSet;

public class URLEncoder implements Serializable {
	private static final long serialVersionUID = 1L;
	public static final URLEncoder DEFAULT = createDefault();
	private final BitSet safeCharacters;

	public static URLEncoder createDefault() {
		URLEncoder encoder = new URLEncoder();
		encoder.addSafeCharacter('-');
		encoder.addSafeCharacter('.');
		encoder.addSafeCharacter('_');
		encoder.addSafeCharacter('~');
		encoder.addSafeCharacter('!');
		encoder.addSafeCharacter('$');
		encoder.addSafeCharacter('&');
		encoder.addSafeCharacter('\'');
		encoder.addSafeCharacter('(');
		encoder.addSafeCharacter(')');
		encoder.addSafeCharacter('*');
		encoder.addSafeCharacter('+');
		encoder.addSafeCharacter(',');
		encoder.addSafeCharacter(';');
		encoder.addSafeCharacter('=');
		encoder.addSafeCharacter(':');
		encoder.addSafeCharacter('@');
		encoder.addSafeCharacter('/');
		return encoder;
	}

	public URLEncoder() {
		this(new BitSet(256));

		char i;
		for(i = 'a'; i <= 'z'; ++i) {
			this.addSafeCharacter(i);
		}

		for(i = 'A'; i <= 'Z'; ++i) {
			this.addSafeCharacter(i);
		}

		for(i = '0'; i <= '9'; ++i) {
			this.addSafeCharacter(i);
		}

	}

	private URLEncoder(BitSet safeCharacters) {
		this.safeCharacters = safeCharacters;
	}

	public void addSafeCharacter(char c) {
		this.safeCharacters.set(c);
	}

	public String encode(String path, Charset charset) {
		StringBuilder rewrittenPath = new StringBuilder(path.length());

		for(int i = 0; i < path.length(); ++i) {
			int c = path.charAt(i);
			if (this.safeCharacters.get(c)) {
				rewrittenPath.append((char)c);
			}
		}
		return rewrittenPath.toString();
	}
}

好的,已收录,谢谢

JEECG开源社区 changed issue state from 待办的 to 已完成

Sign in to comment

状态
Assignees
Milestones
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
Branches
Planed to start   -   Planed to end
-
Top level
Priority
参与者(3)
57093 jeecg 1578915723 1256660 openj2eeforsprog 1578947520
Java
1
https://gitee.com/jeecg/jeecg-boot.git
git@gitee.com:jeecg/jeecg-boot.git
jeecg
jeecg-boot
jeecg-boot

Search