我从文档中了解到,只有当我指定错误的编码作为URLDecoder.decode(String,String)方法的第二个参数时,才会抛出UnsupportedEncodingException.是这样吗?我需要知道可以抛出此异常的情况.
基本上,我在我的一个函数中有这个代码段:
if (keyVal.length == 2) {
try {
value = URLDecoder.decode(
keyVal[1],
"UTF-8");
} catch (UnsupportedEncodingException e) {
// Will it ever be thrown?
}
}
Run Code Online (Sandbox Code Playgroud)
由于我明确提到"UTF-8",有什么办法可以抛出这个异常?我是否需要在catch块中做任何事情?或者,如果我的理解完全错误,请告诉我.
我写的代码应该覆盖所选文本文件的内容,但它会附加它.我究竟做错了什么?
File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");
String source = textArea.getText();
System.out.println(source);
FileWriter f2;
try {
f2 = new FileWriter(fnew,false);
f2.write(source);
/*for (int i=0; i<source.length();i++)
{
if(source.charAt(i)=='\n')
f2.append(System.getProperty("line.separator"));
f2.append(source.charAt(i));
}*/
f2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
编辑
我尝试制作一个新的temp.txt文件并将新内容写入其中,删除此文本文件并将temp.txt重命名为此文件.事实是,删除总是不成功.我不认为我必须为此更改用户权限吗?
此外,我的程序的一部分列出了该目录中的所有文件,因此我猜测它们正在被程序使用,因此无法删除.但为什么不覆盖?
解决了
我最大的"D'哦"时刻!我一直在Eclipse上编译它而不是cmd,这是我执行它的地方.所以我新编译的类转到bin文件夹,通过命令提示符编译的类文件在我的src文件夹中保持不变.我用我的新代码重新编译,它就像一个魅力.
File fold=new File("../playlist/"+existingPlaylist.getText()+".txt");
fold.delete();
File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");
String source = textArea.getText();
System.out.println(source);
try {
FileWriter f2 = new FileWriter(fnew, false);
f2.write(source);
f2.close();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码段
for _, val := range Arr {
// something have to do with val
}
Run Code Online (Sandbox Code Playgroud)
在Arr中,可能有多个元素.我想跳过Arr的第一个元素并从第二个元素继续循环.
例如,如果Arr包含{1,2,3,4}.通过查询我只想采用{2,3,4}.
有没有办法用范围查询来做到这一点?
我有这个InputStream:
InputStream inputStream = new ByteArrayInputStream(myString.getBytes(StandardCharsets.UTF_8));
Run Code Online (Sandbox Code Playgroud)
如何将其转换为ServletInputStream?
我试过了:
ServletInputStream servletInputStream = (ServletInputStream) inputStream;
Run Code Online (Sandbox Code Playgroud)
但是不行.
编辑:
我的方法是这样的:
private static class LowerCaseRequest extends HttpServletRequestWrapper {
public LowerCaseRequest(final HttpServletRequest request) throws IOException, ServletException {
super(request);
}
@Override
public ServletInputStream getInputStream() throws IOException {
ServletInputStream servletInputStream;
StringBuilder jb = new StringBuilder();
String line;
String toLowerCase = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(super.getInputStream()));
while ((line = reader.readLine()) != null) {
toLowerCase = jb.append(line).toString().toLowerCase();
}
InputStream inputStream = new ByteArrayInputStream(toLowerCase.getBytes(StandardCharsets.UTF_8));
servletInputStream = (ServletInputStream) inputStream;
return …Run Code Online (Sandbox Code Playgroud) 在我阅读了有关密码学(对称和非对称)方法的文章之后.很多文章都说私钥用于加密和解密数据.公钥用于加密数据.但是当我尝试用Java开始实现时我不能能够使用私钥来加密和解密数据(我正在使用RSA算法)?如果有可能请给我一个链接.如果它不支持,请回答为什么它不支持?
//加密
Cipher encrypt=Cipher.getInstance("RSA");
encrypt.init(Cipher.ENCRYPT_MODE, privatekey);
byte[] encryptedMessage=encrypt.doFinal(msg.getBytes());
Run Code Online (Sandbox Code Playgroud)
//解密
Cipher decrypt=Cipher.getInstance("RSA");
decrypt.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedMessage=decrypt.doFinal(encryptedMessage);
Run Code Online (Sandbox Code Playgroud) java cryptography encryption-asymmetric encryption-symmetric
码:
String pkcs11cfg = "pkcs11.cfg";
Provider p = new SunPKCS11(pkcs11cfg);
Security.addProvider(p);
KeyStore ks = KeyStore.getInstance("PKCS11", p);
ks.load(null, pin);
System.out.println(ks.size()); // prints 0
Run Code Online (Sandbox Code Playgroud)
CFG:
name = pkcs11Test
library = /usr/local/lib/libsofthsm.so
slot = 1
Run Code Online (Sandbox Code Playgroud)
问题是我有一些密钥对,我用pkcs11-tool添加它们.
softhsm的版本是1.2.1
为什么KeyStore中没有任何别名?如何解决这个问题?
我经常openssl用来生成RSA密钥和证书.但现在我遇到了一个问题.openssl x509 -req需要私钥作为输入.但现在我们正在使用HSM来保护私钥,而我永远无法触及私钥.这样我如何生成x509证书?
我正在使用spring boot版本1.5.6.RELEASE和spring-boot-starter-data-elasticsearch相同版本的应用程序。
我有一个Greeting像这样的模型:
@Document(indexName = "index", type = "greetings")
public class Greeting implements Serializable{
@Id
private Long id;
private String username;
// Getter, Setter and constructor added here
}
Run Code Online (Sandbox Code Playgroud)
对于以下情况,我的控制器和服务类是相同的,没有id.
当我发送以下帖子请求时:
curl -H "Content-Type: application/json" -X POST -d '{"username":"sunkuet02","message": "this is test"}' http://localhost:8080/api/greetings
Run Code Online (Sandbox Code Playgroud)
它回复:
{"id":null,"username":"sunkuet02","message":"this is test"}
Run Code Online (Sandbox Code Playgroud)
然后我更改了Greeting将类型更改id为字符串的类。
@Document(indexName = "index", type = "greetings")
public class Greeting implements Serializable{
@Id
private String id;
private String username; …Run Code Online (Sandbox Code Playgroud) 我在SQL中有一个如下所示的表
我想在PR创建日期或AC创建日期字段中选择具有最早日期的行.所以在这种情况下,我想要这两个记录.
我知道我可以写
Select email, min(PR_create_date) from table
group by email
Run Code Online (Sandbox Code Playgroud)
但是,这只会在PR_create_date列中找到最小日期.如何在PR_create_date或AC_create_date列中找到最小日期?
ElasticSearch 文档引用了一个bin/目录,但我不确定它在哪里。我在 Ubuntu 17.10 和 ES 6.1.1 上运行,以systemctl start elasticsearch.
例如,来自文档:
bin/logstash-plugin list
bin/logstash-plugin list --verbose
bin/logstash-plugin list '*namefragment*'
bin/logstash-plugin list --group output
Run Code Online (Sandbox Code Playgroud)