.
This commit is contained in:
@@ -5,6 +5,7 @@ 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;
|
||||
@@ -16,27 +17,44 @@ import java.util.List;
|
||||
@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) {
|
||||
JBean jBean = new JBean();
|
||||
List list = new ArrayList();
|
||||
String cate = request.getParameter("cate");
|
||||
List data = new ArrayList();
|
||||
try {
|
||||
for (MultiPart part : request.multiParts()) {
|
||||
String filePath = "u/table/" + part.getFilename();
|
||||
File destFile = new File(webroot, filePath);
|
||||
destFile.getParentFile().mkdirs();
|
||||
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);
|
||||
|
||||
list.add(
|
||||
Kv.of("name", part.getFilename())
|
||||
.set("filePath", filePath)
|
||||
.set("viewPath", filePath)
|
||||
);
|
||||
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();
|
||||
}
|
||||
|
||||
response.finish(jBean.set(0, "", list));
|
||||
if ("editor".equalsIgnoreCase(cate)) {
|
||||
response.finish(Kv.of("errno", 0).set("data", data));
|
||||
} else {
|
||||
response.finish(JBean.by(0, "", data));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user