四、编写附件上传的处理惩罚页面,这里利用jsp举办编写,以上的赤色部门,等于要提交到的处理惩罚页面,这里upload.jsp和accessory.html在一个文件夹下,各人也可以放到此外文件夹下,然后改一下上面代码的action值就可以了,upload.jsp内容如下:
<%@ page language=http://down.chinaz.com/try/201106/"java" contentType=http://down.chinaz.com/try/201106/"text/html; charset=UTF-8" pageEncoding=http://down.chinaz.com/try/201106/"UTF-8"%><%@ page import=http://down.chinaz.com/try/201106/"java.util.*,java.io.*" %>
<%@ page import=http://down.chinaz.com/try/201106/"java.text.SimpleDateFormat" %>
<%@ page import=http://down.chinaz.com/try/201106/"org.apache.commons.fileupload.*" %>
<%@ page import=http://down.chinaz.com/try/201106/"org.apache.commons.fileupload.disk.*" %>
<%@ page import=http://down.chinaz.com/try/201106/"org.apache.commons.fileupload.servlet.*" %>
<%@ page import=http://down.chinaz.com/try/201106/"org.json.simple.*" %>
<%
//文件生存目次路径
String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
//文件生存目次URL
String saveUrl = request.getContextPath() + "/attached/";
//界说答允上传的文件扩展名
String[] fileTypes = new String[]{"rar", "zip"};
//最大文件巨细
long maxSize = 1000000;
response.setContentType("text/html; charset=UTF-8");
if(!ServletFileUpload.isMultipartContent(request)){
out.println(getError("请选择文件。"));
return;
}
//查抄目次
File uploadDir = new File(savePath);
if(!uploadDir.isDirectory()){
out.println(getError("上传目次不存在。"));
return;
}
//查抄目次写权限
if(!uploadDir.canWrite()){
out.println(getError("上传目次没有写权限。"));
return;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
String fileName = item.getName();
long fileSize = item.getSize();
if (!item.isFormField()) {
//查抄文件巨细
if(item.getSize() > maxSize){
out.println(getError("上传文件巨细高出限制。"));
return;
}
//查抄扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if(!Arrays.<String>asList(fileTypes).contains(fileExt)){
out.println(getError("上传文件扩展名是不答允的扩展名。"));
return;
}
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
try{
File uploadedFile = new File(savePath, newFileName);
item.write(uploadedFile);
}catch(Exception e){
out.println(getError("上传文件失败。"));
return;
}
JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", saveUrl + newFileName);
obj.put("filename",fileName);
out.println(obj.toJSONString());
}
}
%>
<%!
private String getError(String message) {
JSONObject obj = new JSONObject();
obj.put("error", 1);
obj.put("message", message);
return obj.toJSONString();
}
%>
五、修改skins目次里的default.gif,增加附件图标,可回收photoshop等图片处理惩罚软件,在default.gif的最后头加一个附件图标,巨细为16*16px的即可
六、修改skins目次里的default.css,插手
.ke-icon-accessory{
background-position: 0px -960px;
width: 16px;
height: 16px;
}
七、测试结果如下: