61 lines
2.0 KiB
Java
61 lines
2.0 KiB
Java
package net.tccn.file;
|
|
|
|
import net.tccn.base.BaseServlet;
|
|
import net.tccn.base.JBean;
|
|
import net.tccn.base.Kv;
|
|
import org.redkale.net.http.*;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @author: liangxianyou at 2018/10/11 17:08.
|
|
*/
|
|
@WebServlet(value = {"/upload/*"}, comment = "测试servlet")
|
|
public class FileServlet extends BaseServlet {
|
|
|
|
@Resource(name = "property.upfile.dir")
|
|
private String dir = "";
|
|
@Resource(name = "property.upfile.view")
|
|
private String view = "";
|
|
|
|
private String format = "%1$tY%1$tm%1$td%1$tH%1$tM%1$tS";
|
|
|
|
@HttpMapping(url = "/upload/", comment = "文件上传,访问地址:/upload/x")
|
|
public void uploadExcel(HttpRequest request, HttpResponse response) {
|
|
String cate = request.getParameter("cate");
|
|
List data = new ArrayList();
|
|
try {
|
|
for (MultiPart part : request.multiParts()) {
|
|
String name = part.getFilename();
|
|
String suffix = name.substring(name.lastIndexOf("."));
|
|
String path = String.format(format, System.currentTimeMillis()) + suffix;
|
|
File destFile = new File((winos ? webroot + "/tem/" : dir) + path);
|
|
|
|
if (!destFile.getParentFile().exists()) {
|
|
destFile.getParentFile().mkdirs();
|
|
}
|
|
part.save(destFile);
|
|
|
|
String viewPath = (winos ? "/tem/" : view) + path;
|
|
if ("editor".equalsIgnoreCase(cate)) {
|
|
data.add(viewPath);
|
|
} else {
|
|
data.add(Kv.of("name", part.getFilename()).set("filePath", viewPath).set("viewPath", viewPath));
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if ("editor".equalsIgnoreCase(cate)) {
|
|
response.finish(Kv.of("errno", 0).set("data", data));
|
|
} else {
|
|
response.finish(JBean.by(0, "", data));
|
|
}
|
|
}
|
|
}
|