廣告

2016年9月22日 星期四

[java] multipart/form-data POST request using Java  | mailgun with attachment


        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        credsProvider.setCredentials(

                new AuthScope(HOST_NAME, PORT),

                new UsernamePasswordCredentials(USER_NAME, TOKEN));

        CloseableHttpClient httpclient = HttpClients.custom()

                .setDefaultCredentialsProvider(credsProvider)

                .build();

        try {

            HttpPost httpPost = new HttpPost(BASE_URL + EMAIL_API_NAME);


            MultipartEntityBuilder builder = MultipartEntityBuilder.create();


            builder.addBinaryBody("attachment", new File("D:/opt/test.pdf"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");

            builder.addTextBody("from", FROM_ADDRESS, ContentType.APPLICATION_JSON);

            builder.addTextBody("to", toEmail, ContentType.APPLICATION_JSON);

            builder.addTextBody("bcc", bcc, ContentType.APPLICATION_JSON);

            builder.addTextBody("subject", subject, ContentType.APPLICATION_JSON);

            builder.addTextBody("html", this.loadTemplate(template, values), ContentType.APPLICATION_JSON);

            HttpEntity multipart = builder.build();

            httpPost.setEntity(multipart);

            CloseableHttpResponse response2 = httpclient.execute(httpPost);

            log.info(response2.toString());


            try {

                System.out.println(response2.getStatusLine());

                HttpEntity entity2 = response2.getEntity();

                // do something useful with the response body

                // and ensure it is fully consumed

                EntityUtils.consume(entity2);

            } finally {

                response2.close();

            }

        } finally {

            httpclient.close();

        }


ref:http://stackoverflow.com/questions/1378920/how-can-i-make-a-multipart-form-data-post-request-using-java

[java] isKeysExistInMap (keys ,map )



package test;

import java. util. ArrayList;
import java. util. HashMap;
import java .util . List;

public class testMapContainMultiKey {

       public static void main( String[] args ) {
             // TODO Auto-generated method stub

             ArrayList< String> keys = new ArrayList< String> ();
            keys .add ("a" );
            keys .add ("b" );

             HashMap< String, String > map = new HashMap<> ();
            map .put ("a" , "a" );
            map .put ("c" , "b" );

             System. out .println ( isKeysExistInMap (keys ,map ) );


       }

       public static boolean  isKeysExistInMap( ArrayList< String> keys , HashMap  map ) {
             boolean result = true;
             for (String key : keys) {
                 result = map. containsKey (key );
                 if (result == false ) {
                     return result;
                 }
             }
             return result;
       }

}


2016年8月10日 星期三

a bug in Chrome's live-edit CSS/JS. Notice how the number changes on each request.

Error msg:
Error shows “Failed to get text for stylesheet (#): No style sheet with given id found”

Ans:

closed all the files in the 'source' tab.


2016年8月7日 星期日

[git]Untrack files from git

This will tell git you want to start ignoring the changes to the file
git update-index --assume-unchanged path/to/file

When you want to start keeping track again
git update-index --no-assume-unchanged path/to/file

usage occasion : put some file in remote once and not track it anymore

                 ex:local config

2016年7月13日 星期三

2016年6月23日 星期四

Hibernate Eager vs Lazy Fetch Type


  @OneToOne(fetch=FetchType.EAGER) //LAZY
  @JoinColumn(name="user_profile_id")
  private Profile getUserProfile()
  {
   return userProfile;
  }

 FetchType.LAZY = Doesn’t load the relationships 
 unless explicitly “asked for” via getter
 FetchType.EAGER = Loads ALL relationships

ref:
https://m.oschina.net/blog/327056
https://howtoprogramwithjava.com/hibernate-eager-vs-lazy-fetch-type/

2016年6月8日 星期三

Hibernate paging get error result if row have same value


Query queryHot = session.createQuery(
   "from HotPost ORDER BY popularity DESC");

queryHot.setFirstResult((page - 1) * pageSize);
queryHot.setMaxResults(pageSize);

solve:
Query queryHot = session.createQuery(
   "from HotPost ORDER BY popularity DESC,id");
==================================
Before:
id   popularity 
1     9   |
2     8   |  page 1
3     8   |

5     8   |
6     7   |  page 2
7     5   |

AFTER:
id   popularity 
1     9  |
2     8  |  page 1
3     8  |

4     8  |
5     7  |  page 2
6     5  |