/**
* 上传关卡图片,保存数据库路径
* @throws IOException
* @throws ServletException
*/
private String uploadPath = "image"; // 上传关卡图片的目录
private String tempPath = "imagetmp/"; // 临时文件目录
private String uploadPath1 = "audio"; // 上传音频的目录
private String uploadPath2 = "video"; // 上传视频的目录
private String uploadPath3 = "simage";//上传上神途径的目录
private String uploadPath4 = "mimage";//上传护法图片目录
private String serverPath = null;
private int sizeMax1 = 50;//音频最大上限
private int sizeMax2 = 200;//视频最大上限
private int sizeMax = 3;//图片最大上限
private String[] fileType = new String[]{".jpg",".gif",".bmp",".png",".jpeg",".ico"};
private String[] fileType1 = new String[]{".mp3",".wav",".aac",".ogg",".wma",".flac",".ape"};
private String[] fileType2 = new String[]{".rm",".rmvb",".mpeg", ".wmv",".avi",".3gp",".amv","mp4"};
private String select1 = "";//前端传过来的参数用来拼接路径
private String select2 = "";
private String select3 = "";
private String select = "";
@RequestMapping(value="upload",method=RequestMethod.POST)
public void upLoad(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
CPInfoPO cpInfoPO = new CPInfoPO();
serverPath = request.getServletContext().getRealPath("/").replace("\\", "/");
//Servlet初始化时执行,如果上传文件目录不存在则自动创建
if(!new File(serverPath+uploadPath).isDirectory()){
new File(serverPath+uploadPath).mkdirs();
}
if(!new File(serverPath+tempPath).isDirectory()){
new File(serverPath+tempPath).mkdirs();
}
//String select1 = multipartRequest.getParameter("select1");
//String select2 = multipartRequest.getParameter("select2");
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(5*1024); //最大缓存
factory.setRepository(new File(serverPath+tempPath));//临时文件目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(sizeMax*1024*1024);//文件最大上限
String filePath = null;
try {
List<FileItem> items = upload.parseRequest(request);//获取所有文件列表
Map param = new HashMap();
for (FileItem item : items) { //表示普通标签域
//获得文件名,这个文件名包括路径
if(item.isFormField()){
param.put(item.getFieldName(), item.getString("utf-8"));
//System.out.println(select = select1+"_"+select2);
}}
select2 = (String)param.get("select2");
select1 = (String)param.get("select1");
select = select1+"_"+select2;
for (FileItem item : items){
if(!item.isFormField()){ //表示上传文件标签域
//文件名
String fileName = item.getName().toLowerCase();
if(fileName.endsWith(fileType[0])||fileName.endsWith(fileType[1])||fileName.endsWith(fileType[2])||fileName.endsWith(fileType[3])||fileName.endsWith(fileType[4])||fileName.endsWith(fileType[5])){
//String uuid = UUID.randomUUID().toString();
//HttpSession session = request.getSession();
filePath = serverPath+uploadPath+"\\"+select+fileName.substring(fileName.lastIndexOf("."));
item.write(new File(filePath));
//System.out.println(filePath);
String url=uploadPath+"\\"+select+fileName.substring(fileName.lastIndexOf("."));
PrintWriter pw = response.getWriter();
request.setAttribute("result", "上传成功!");
request.getRequestDispatcher("/admin/uploadCPImage").forward(request,
response);
pw.flush();
pw.close();
}else{
request.setAttribute("result", "上传失败,请确认上传的文件存在并且类型是图片!");
request.getRequestDispatcher("/admin/uploadCPImage").forward(request,
response);
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("result", "上传失败,请确认上传的文件大小不能超过"+sizeMax+"M");
request.getRequestDispatcher("/admin/uploadCPImage").forward(request,
response);
}
}
需引入common-fileupload.jar上传图片,音频,视频均可用。
最近在整理一些资源工具,放在网站分享 http://tools.maqway.com
欢迎关注公众号:麻雀唯伊 , 不定时更新资源文章,生活优惠,或许有你想看的