小编Ted*_*pin的帖子

简单的Java AES加密/解密示例

以下示例有什么问题?

问题是解密字符串的第一部分是无意义的.但是,剩下的很好,我明白了......

Result: `£eB6O?geS??i are you? Have a nice day.
Run Code Online (Sandbox Code Playgroud)
@Test
public void testEncrypt() {
  try {
    String s = "Hello there. How are you? Have a nice day.";

    // Generate key
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);
    SecretKey aesKey = kgen.generateKey();

    // Encrypt cipher
    Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    encryptCipher.init(Cipher.ENCRYPT_MODE, aesKey);

    // Encrypt
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, encryptCipher);
    cipherOutputStream.write(s.getBytes());
    cipherOutputStream.flush();
    cipherOutputStream.close();
    byte[] encryptedBytes = outputStream.toByteArray();

    // Decrypt cipher
    Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivParameterSpec = …
Run Code Online (Sandbox Code Playgroud)

java encryption aes

110
推荐指数
5
解决办法
46万
查看次数

jersey - StreamingOutput作为响应实体

我在Jersey Resource类中实现了流输出.

@GET
@Path("xxxxx")
@Produces(BulkConstants.TEXT_XML_MEDIA_TYPE})   
public Response getFile() {

    FeedReturnStreamingOutput sout = new FeedReturnStreamingOutput();
    response = Response.ok(sout).build();
    return response;
}

class FeedReturnStreamingOutput implements StreamingOutput {

    public FeedReturnStreamingOutput()

    @Override
    public void write(OutputStream outputStream)  {
        //write into Output Stream
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是尽管在调用FeedReturnStreamingOutput之前从资源发回响应但是Jersey客户端等待直到FeedReturnStreamingOutput执行完成.

客户代码:

Client client = Client.create();

ClientResponse response = webResource
    //headers
    .get(ClientResponse.class);

//The codes underneath executes after FeedReturnStreamingOutput is executed which undermines the necessity of streaming

OutputStream os = new FileOutputStream("c:\\test\\feedoutput5.txt");
System.out.println(new Date() + " : Reached point A");

if (response.getStatus() …
Run Code Online (Sandbox Code Playgroud)

java jersey jersey-client

22
推荐指数
1
解决办法
2万
查看次数

如何反序列化空数组/列表?

我有一个房产

@JsonProperty
private Map<String, String> parameters = new HashMap<String, String>();
Run Code Online (Sandbox Code Playgroud)

当我尝试通过调用objectMapper.readValue(...)进行反序列化时,一切正常,直到JSON中的参数字段为空,即.

"parameters":[]
Run Code Online (Sandbox Code Playgroud)

我得到这个例外......

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.HashMap out of START_ARRAY token
Run Code Online (Sandbox Code Playgroud)

我该如何处理空列表?不,我对JSON的控制没有任何控制权.

谢谢.

json jackson

5
推荐指数
1
解决办法
2351
查看次数

未设置os.detected.classifier-os-maven-plugin和蚀氧

我正在忍受无法解决的maven-protoc-plugin问题,os.detected.classifier导致日食报告我的pom错误。

我找到了此修复程序,但我怀疑它仅适用于较旧的Eclipse版本,<eclipse>/plugins氧气中不再有文件夹。

我尝试os.detected.classifier在eclipse.ini和Windows环境变量中进行设置均无济于事。

这是一些认为会有所帮助的pom的剪辑。

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.3.0.Final</version>
        </extension>
    </extensions>             
    <plugins>
         <plugin>
            <groupId>com.google.protobuf.tools</groupId>
            <artifactId>maven-protoc-plugin</artifactId>
            <version>0.4.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
Run Code Online (Sandbox Code Playgroud)

还有部分错误...

Missing:
----------
1) com.google.protobuf:protoc:exe:${os.detected.classifier}:2.6.1

Try downloading the file manually from the project website.
Run Code Online (Sandbox Code Playgroud)

java eclipse eclipse-plugin maven

5
推荐指数
3
解决办法
1539
查看次数

如何从线程返回值(java)

我做了一个像下面这样的线程:

public class MyThread implements Runnable {
  private int temp;

  public MyThread(int temp){
     this.temp=temp;
  }

  @Override
  public void run() {
    temp+=10;
    return;
  }

  public int getTemp() {
    return temp;
  }
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试通过 getTemp 使用 temp 时,我得到 0

class Main {
  MyThread foo = new MyThread(10);
  Thread a = new Thread(foo);
  a.start();
  int aa = foo.getTemp();       
  System.out.println(aa);
}
Run Code Online (Sandbox Code Playgroud)

我只想使用我在线程中所做的计算存储在一些变量中以备后用。

java multithreading

2
推荐指数
1
解决办法
7907
查看次数

React-bootstrap - Form.Control defaultValue 重新渲染时未更新

当我调用setState并且重新呈现表单时,文本输入的值不会更改。

import React, { Component } from 'react';
import { Button, Col, Form, Row } from 'react-bootstrap';

export default class Test extends Component {

    constructor(props) {
        super(props);
        this.state = {
            status: "new"
        }
    }

    approve = (e) => {
        this.setState({
            status: "I'm newer than you!"
        });
        e.preventDefault();
    }

    render() {
        return (
            <div>
                Status is {this.state.status}
                <Form onSubmit={this.approve}>
                    <Form.Group as={Row} controlId="status">
                        <Form.Label column >Status</Form.Label>
                        <Col>
                            <Form.Control readOnly type="text" size="sm" defaultValue={this.state.status} />
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row}>
                        <Button type="submit">approve</Button>
                    </Form.Group> …
Run Code Online (Sandbox Code Playgroud)

forms reactjs react-bootstrap

2
推荐指数
1
解决办法
3446
查看次数

Jersey获得HTTP 405,未按预期处理路径映射

我有以下2种方法...

@GET
@Path("/{solution}")
public Response test(@PathParam("solution") String solution,
        @Context UriInfo uriInfo, @Context HttpHeaders headers);

@GET
@Path("/{solution}/{path:[a-z0-9/\\-]*}")
public Response testTest(@PathParam("solution") String solution,
        @PathParam("path") String nodePath,
        @Context UriInfo uriInfo, @Context HttpHeaders headers);
Run Code Online (Sandbox Code Playgroud)

当我调用/ my-app / test / test时,第二个方法被调用并且参数设置正确。当我调用/ my-app / test而不是第一个方法被调用时,我收到未找到的HTTP 405方法。我假设它正在将URL映射到其他方法之一,例如。

@DELETE
@Path("{path: [a-z0-9/\\-]*}")
public Response deleteTest(@PathParam("path") String path, @Context HttpHeaders headers);
Run Code Online (Sandbox Code Playgroud)

有人发现我做错了吗?任何人都可以找到jersey试图映射到的方法的任何提示?

谢谢。

java jersey

1
推荐指数
1
解决办法
2578
查看次数