Java圖片上傳
阿新 • • 發佈:2018-02-07
ack 連接 ati pat html out eclips ppi substring 最近閑來無事,就整理常用的功能,以下是項目中圖片上傳的功能,將圖片上傳到服務器tomcat下,數據庫中存放相對路徑,大家一起學習。
環境:eclipse jdk1.7 tomcat7 maven3.5
步驟:
@RequestMapping(params="upLoadPicture") @ResponseBody public String addFilemsgPic(MultipartFile file,HttpServletRequest request) throws Exception{ String path="../Manage_ssm/upLoadPicture"; //創建文件 File dir=new File(path); if(!dir.exists()){ dir.mkdirs(); } String username=(String) request.getSession().getAttribute("userName"); //返回原來在客戶端的文件系統的文件名 String fileName=file.getOriginalFilename(); //username+後綴名 String img=username+fileName.substring(fileName.lastIndexOf("."));//zhao.jpg FileOutputStream imgOut=new FileOutputStream(new File(dir,img));//根據 dir 抽象路徑名和 img 路徑名字符串創建一個新 File 實例。 imgOut.write(file.getBytes());//返回一個字節數組文件的內容 imgOut.close(); Map<String, String> map=new HashMap<String, String>(); String rpath = path+"/"+img; map.put("rPath",rpath); JSONObject jsonObject = JSONObject.fromObject(map);//將json字符串轉換為json對象 String r=jsonObject.toString(); String s=URLEncoder.encode(r, "utf-8");//加密 return s; }
@RequestMapping(params="pictureToDb") @ResponseBody public int pictureToDb(String src,HttpServletRequest request){ String newSrc = src.replace(" ", "+"); int flag=ConnOrcl.connToTable(newSrc,request); if(flag==1) return 1; else return 2; }
public class ConnOrcl { private static Connection conn; private static Statement stat; private static String driver = "oracle.jdbc.driver.OracleDriver"; private static int SUCCESS=1; private static int FILE=2; public static int connToTable(String file,HttpServletRequest request) { try { //數據庫連接 Class.forName(driver); conn = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "GKBMFZXT", "GKBMFZXT"); stat = conn.createStatement(); String username=(String) request.getSession().getAttribute("userName"); String sql="update user_table set picture='"+file+"' where user_name='"+username+"'"; stat.execute(sql.toString()); return SUCCESS; } catch(ClassNotFoundException e) { e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } finally { if(null != stat) { try { stat.close(); } catch (SQLException e) { e.printStackTrace(); } } if(null != conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } return FILE; } }
<script src="js/ajaxfileupload.js"></script> <script type="text/javascript"> function upload(){ var file="file"; var picElement=document.getElementById(file).value; var picFormat=picElement.substring(picElement.lastIndexOf('.')+1,picElement.length); if(picFormat!="jpg"){ alert("請選擇jpg 格式的文件!"); return; } $.ajaxFileUpload({ url:"user.do?upLoadPicture", secureuri: false, fileElementId:file, dataType: 'JSON', success:function(data){ data=decodeURIComponent(data);//解碼 var arr=JSON.parse(data);//JSON.parse() 方法解析一個JSON字符串 $("#fsrc").val(arr.rPath); $.post( "user.do?pictureToDb&src="+$("#fsrc").val(), function(data){ //data=decodeURIComponent(data); //var arr=JSON.parse(data); if(data==1){ alert("上傳成功!"); }else{ alert("上傳失敗!"); } } ); }, error: function (){ alert("上傳失敗!當前時間為:"+new Date().toLocaleTimeString()); } }); }; function findFile(){ document.getElementById("file").click(); }
Java圖片上傳