How to import txt file with UTF-8 encoding into jsp?

Hey I’m working on a web application and have problems with read UTF-8 chars from txt files. I get UTF-8 working that way: UTF-8 web encoding (and it workes fine except at the import). I tryed a lot of thinks (especially from: read UTF-8 string literal java) but nothing work and I have no idea why.

The importent codesnippets:

import.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Import</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="script.js"></script>
<link rel="stylesheet" type="text/css"
media="screen and (min-device-width: 500px)" href="style.css" />
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<form>
    <!-- show import data -->
</form>
<form id="importForm" action="${pageContext.request.contextPath}/ImportData" method="post" onsubmit="return importValidation();" enctype="multipart/form-data">
    <input type="file" name="file" accept=".txt"/>
    <input type="submit" value="Import">
</form>

</body>
</html>

ImportData Servlet:

import java.nio.charset.StandardCharsets;

@WebServlet("/ImportData")
@MultipartConfig
public class ImportData extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
         BufferedReader buf = new BufferedReader(new InputStreamReader(filePart.getInputStream(), StandardCharsets.UTF_8.name()));
         String lineJustFetched = null;
         String[] wordsArray = null;
         ArrayList<String> texts = new ArrayList<String>();
         while(true){
             lineJustFetched = buf.readLine();
             if(lineJustFetched == null){  
                 break; 
             }else{
                 wordsArray = lineJustFetched.split("t");
                 for(String each : wordsArray){
                     texts.add(each);
                 }
             }
         }
         buf.close();

        System.out.println(texts);

        //create Import Data in Backend and write it into db

        response.sendRedirect("import.jsp");
    }
}

System details:
Tomcat server 7 with Java 1.7

The outprint of texts for UTF-8 chars is a square and in html inputs (and texts) is a � instead of the UTF-8 chars

So my question is: Where and why do I lost the UTF-8 encoding?

java generic object istance of generic class

I have this situation:

I receive a string from JSon file and I parse it correclty into a list, so at the end I have a list of myObject

I want create a generic method to search one thing into this list. I would to create a method that has how parameter one generic list of object and the Type of this object, after during the scan of the list, cast this generic object into the type that is passed.

List<Mission_queue> queue = gson.fromJson((String)response.get(1),listType);
Utility.CheckProperty(queue, Mission_queue.class, paramName, paramSearch);

My Utility method

public static Boolean CheckProperty(List<?> list, Class<?> classType, String propertyName, String propertyValue) {
int idx = 0;
    for (Iterator<?> it = list.iterator(); it.hasNext(); idx++) {
        Object obj = it.next();
        // here I want cast my generic Obj from Object to ClassType
    }
    return false;

So, how could I cast? I would create a generic method because I will have a lot of different list, but every time I need to search something inside.

Have you some ideas?

Thanks very much

What’s the best practice to put stuff under resources folder when using Spring Boot

I’m new to SpringBoot and glanced over the spring boot tutorial recently.
According to the tutorial from Serving Web Content with Spring MVC, I found there are static and templates folder under resources folder.

For the static folder, there is introduction below:

Static resources, like HTML or JavaScript or CSS, can easily be served
from your Spring Boot application just be dropping them into the right
place in the source code. By default Spring Boot serves static content
from resources in the classpath at “/static” (or “/public”).

For the templates folder, I think it’s for server-side rendering template engine like FreeMarker, JSP , Thymeleaf and so on.

For some examples from github, the rule is followed strictly, take https://github.com/evgeniyJ/springboot/tree/master/src/main/resources for instance.

But some other example may be a little different, like this one
https://github.com/JeffLi1993/springboot-learning-example/tree/master/springboot-freemarker/src/main/resources, it takes the template files under web folder.

Besides that, both example have more content than static folder and templates folder, like sql folder and some properties file.

So here comes my question:
1, Is the rule about static folder and template folder the best practice?
2, Can I reach the conclusion that any thing not pure java files should be put under resources folder?

Thanks for your clarification in advance.

How can I handel a Json response which have a Timestamp using the Gson ?? java.lang.IndexOutOfBoundsException,

I have an object which have a 3 Timestamp fields and i want to consume a json response using Gson but i have got an Invalid time zone indicator ‘ ‘.
so this is out put :

    com.google.gson.JsonSyntaxException: 2017-11-30 15:19:37
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:84)

... 31 more Caused by: java.lang.IndexOutOfBoundsException: Invalid time zone indicator ' '
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:245)
... 32 more
Caused by: java.lang.IndexOutOfBoundsException: Invalid time zone indicator ' '
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:245)
... 32 more

And this is my Json response :

{
"status": "OK",
"data": {
    "due_days": 10,

    "attested_at": null,
    "updated_at": "2017-12-01 11:59:56",
    "automatic_collection": {
        "amount": null,
        "delay_days": null
    },
    "currency": "SEK",
        "collector_paid_sum": 0,
        "remaining_sum": 0
    },
    "id": "zeY37WJ",
    "reminder_fee": 60.0,
    "interest_rate": 8.5,
    "state": "Unattested",
    "due_date": "2017-12-11",
    "automatic_reminders_settings": [],
    "invoice_fee_vat": 0,
    "invoice_date": "2017-12-01",
    "reminder_count": 0,
    "interest_fee": 0.0,
    "invoice_no": 0,
    "created_at": "2017-11-30 15:21:08",
    "total_sum": 0.0,
    "delivery_method": "Letter",
    "remaining_sum": 0.0,
    "flags": [],
}

}

THis my Gson code :

GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.registerTypeAdapter(Timestamp.class, new DateDeserializer()).create();
    invoiceModels = gson.fromJson(jsonObject.get("data").toString(), InvoiceModel[].class);

and this is my DateDeserializer

public class DateDeserializer implements JsonDeserializer<Date> {
public Date deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
  String date = element.getAsString();

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ'");
  format.setTimeZone(TimeZone.getTimeZone("GMT"));

  try {
      return format.parse(date);
  } catch (ParseException exp) {
      System.out.println("Failed to parse Date:"+ exp);
      return null;
  }}}

Regular expression to match all java operators in a string

I’m working on lexical analyzing in java world, and want to break a given string into tokens discarding the spaces. I use the below regex to match tokens such as alphabet, numbers and the most common operators and separators:

"[a-zA-Z0-9_]|[\[\](){}.;,!<>+^%]"

However, operators like ++ , -- , ==,<=,>= ^=,*=,+= is difficult to handle. Any help in how to improve my regex to fit my needs ? Many thanks.

Google Play Games

Good day everyone.

I’m trying to implement Achievements in a game I’m developing.

I already set everything on google play console, got the app-id, put in the manifest the following

    <meta-data
        android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />

and wrote the following method

        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        int temp = googleApiAvailability.isGooglePlayServicesAvailable(this);
        if ( temp != 0)
            return;

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
                .requestEmail()
                .build();

        GoogleSignIn.getClient(this, gso);

        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        PlayersClient player = Games.getPlayersClient(this, account);

When I run it I get my account, but as it runs Games.getPlayersClient(this, account); I get the following error:

java.lang.IllegalStateException: Games APIs requires
https://www.googleapis.com/auth/games_lite function.

Anybody as any idea what could be wrong?

Thanks in advance.

Apache POI update formula references when copying

Is there a way to update formula references when copying a formula in Apache POI?

Say in Excel you have in row 1 the formula =A1/B1. If you copy-paste it, say in row 5, the formula becomes =A5/B5.

In Apache POI if you run the lines

r5.getCell(2).setCellType(CellType.FORMULA);
r5.getCell(2).setCellFormula(r1.getCell(2).getCellFormula());

the formula remains =A1/B1.