• java实现上传图片


    1.将图片上传到tomcat下

    2.将相对路径存放到数据库中

     1 @RequestMapping(params="upLoadPicture") 
     2     @ResponseBody
     3     public String addFilemsgPic(MultipartFile file,HttpServletRequest request) throws Exception{
     4         
     5         String path="../Manage_ssm/upLoadPicture";
     6         
     7         //创建文件 
     8         File dir=new File(path);
     9         if(!dir.exists()){
    10             dir.mkdirs();
    11         }
    12         
    13         String username=(String) request.getSession().getAttribute("userName");
    14         //返回原来在客户端的文件系统的文件名
    15         String fileName=file.getOriginalFilename();
    16         //username+后缀名
    17     
    18         String img=username+fileName.substring(fileName.lastIndexOf("."));//zhao.jpg
    19         FileOutputStream imgOut=new FileOutputStream(new File(dir,img));//根据 dir 抽象路径名和 img 路径名字符串创建一个新 File 实例。
    20         
    21         imgOut.write(file.getBytes());//返回一个字节数组文件的内容
    22         imgOut.close();
    23         Map<String, String> map=new HashMap<String, String>();
    24     
    25         String rpath = path+"/"+img;
    26         map.put("rPath",rpath);
    27         JSONObject jsonObject = JSONObject.fromObject(map);//将json字符串转换为json对象
    28         String r=jsonObject.toString();
    29         String s=URLEncoder.encode(r, "utf-8");//加密
    30         return s;
    31     }
     1 @RequestMapping(params="pictureToDb") 
     2     @ResponseBody
     3     public int pictureToDb(String src,HttpServletRequest request){
     4     
     5        
     6         String newSrc = src.replace(" ", "+");
     7         int flag=ConnOrcl.connToTable(newSrc,request);
     8         if(flag==1)
     9             return 1;
    10         else
    11             return 2;
    12     }
     1 public class ConnOrcl {
     2     
     3         private static Connection conn;  
     4         private static Statement stat;  
     5         private static String driver = "oracle.jdbc.driver.OracleDriver";  
     6         private static int SUCCESS=1; 
     7         private static int FILE=2; 
     8         public static int connToTable(String file,HttpServletRequest request)  
     9         {  
    10             try  
    11             {  
    12                 //数据库连接  
    13                 Class.forName(driver);  
    14                 conn = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "GKBMFZXT", "GKBMFZXT");  
    15                 stat = conn.createStatement();  
    16                
    17                 String username=(String) request.getSession().getAttribute("userName");             
    18                 String sql="update user_table set picture='"+file+"' where user_name='"+username+"'";      
    19                 stat.execute(sql.toString());  
    20                    
    21                 return SUCCESS;
    22             }  
    23             catch(ClassNotFoundException e)  
    24             {  
    25                 e.printStackTrace();  
    26             }  
    27             catch(SQLException e)  
    28             {  
    29                 e.printStackTrace();  
    30             }  
    31             finally  
    32             {  
    33                 if(null != stat)  
    34                 {  
    35                     try   
    36                     {  
    37                         stat.close();  
    38                     }   
    39                     catch (SQLException e)  
    40                     {  
    41                         e.printStackTrace();  
    42                     }  
    43                 }  
    44                 if(null != conn)  
    45                 {  
    46                     try   
    47                     {  
    48                         conn.close();  
    49                     }   
    50                     catch (SQLException e)   
    51                     {  
    52                         e.printStackTrace();  
    53                     }  
    54                 }  
    55             }  
    56             return FILE;
    57         }  
    58 
    59 }
     1 <script src="js/ajaxfileupload.js"></script>
     2 <script type="text/javascript">
     3 function upload(){
     4     
     5     var file="file";
     6     var picElement=document.getElementById(file).value;
     7     
     8     var picFormat=picElement.substring(picElement.lastIndexOf('.')+1,picElement.length);
     9     if(picFormat!="jpg"){
    10         alert("请选择jpg 格式的文件!");
    11         return;
    12     }
    13 
    14     $.ajaxFileUpload({
    15              url:"user.do?upLoadPicture",
    16            secureuri: false,
    17            fileElementId:file,
    18            dataType: 'JSON',
    19            success:function(data){
    20                   data=decodeURIComponent(data);//解码
    21                   var arr=JSON.parse(data);//JSON.parse() 方法解析一个JSON字符串            
    22                 $("#fsrc").val(arr.rPath);         
    23                  $.post(
    24                         "user.do?pictureToDb&src="+$("#fsrc").val(),    
    25                         function(data){
    26                             //data=decodeURIComponent(data);
    27                             //var arr=JSON.parse(data);
    28                             if(data==1){
    29                             alert("上传成功!");
    30                             }else{
    31                             alert("上传失败!");
    32                             }
    33 
    34                         }
    35                     ); 
    36         },
    37         error: function (){
    38                alert("上传失败!当前时间为:"+new Date().toLocaleTimeString());
    39           
    40        }
    41     });
    42 };
    43 
    44 function findFile(){
    45     document.getElementById("file").click();
    46 }
  • 相关阅读:
    2013.11.18 流水
    return to blog!
    IOS实现毛玻璃效果的三种方式
    UI常用控件总结(三)
    UI常用控件总结(二)
    UI常用控件总结(一)
    UIView 常见属性
    OC语言BLOCK和协议
    OC语言类的深入和分类
    OC语言构造方法
  • 原文地址:https://www.cnblogs.com/xiaotian-222/p/6962351.html
Copyright © 2020-2023  润新知