直接發代碼了 這是我一直以來使用的上傳代碼
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.json.simple.*" %>
<%
//擷取action路徑,圖片存放路徑
//String actionUrl=request.getParameter("actionUrl");
String imageUrl=request.getParameter("folderName");
//System.out.println("檔案的目錄路徑是"+imageUrl);
String resultMsg=null;
//檔案儲存目錄路徑
String savePath = pageContext.getServletContext().getRealPath("/") + imageUrl+"/";
String url = this.getClass().getClassLoader().getResource("/")
.toString().replace("WEB-INF/classes/", "").replace("file:/", "");
//System.out.println("檔案的目錄路徑是"+savePath);
//檔案儲存目錄URL
String saveUrl = request.getContextPath() + "/"+imageUrl+"/";
//檔案名稱
String newFileName="";
//檔案大小
long fileSize=0;
//定義允許上傳的副檔名
String[] fileTypes = new String[]{"gif", "jpg", "jpeg", "png", "bmp","GIF","JPG","JPEG","PNG","BMP"};
//最大檔案大小
long maxSize = 1000000;
response.setContentType("text/html; charset=UTF-8");
if(!ServletFileUpload.isMultipartContent(request)){
resultMsg="請選擇檔案!";
}else{
//檢查目錄
File uploadDir = new File(savePath);
if(!uploadDir.isDirectory()){
out.println(savePath);
resultMsg="上傳目錄不存在!";
uploadDir.mkdir();
}else{
if(!uploadDir.canWrite()){
resultMsg="上傳目錄沒有讀寫權限!";
}else{
//建立檔案夾
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String ymd = sdf.format(new Date());
savePath += ymd + "/";
saveUrl += ymd + "/";
File dirFile = new File(savePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
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();
fileSize = item.getSize();
if (!item.isFormField()) {
//檢查檔案大小
if(item.getSize() > maxSize){
resultMsg="上傳檔案大小超過限制!";
}else{
//檢查副檔名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if(!Arrays.<String>asList(fileTypes).contains(fileExt)){
resultMsg="上傳副檔名是不允許的副檔名!";
}else{
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
try{
File uploadedFile = new File(savePath, newFileName);
item.write(uploadedFile);
}catch(Exception e){
resultMsg="上傳檔案失敗!";
}
}
}
}
}
}
}
}
String uploadUrl=saveUrl+newFileName;
System.out.print(newFileName);
if(uploadUrl.indexOf(".")<0){
uploadUrl="";
}
try {
if(resultMsg!=null){
response.getWriter().print("<script>alert(\""+resultMsg+"\");</script>");
}
String resultStr="{\"id\":\""+1000+"\",\"fileName\":\""+newFileName+"\",\"attachmentPath\":\""+uploadUrl+"\",\"attachmentSize\":\""+fileSize+"\"}";
response.getWriter().print(resultStr);
} catch (IOException e) {
e.printStackTrace();
}
%>
由於配合使用了dwz架構 要求資料格式是json格式 所以裡面的一些提示資訊改成這樣 但是應該不影響試用 只要稍微調整一下就好