-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperations.java
More file actions
74 lines (62 loc) · 2.8 KB
/
Copy pathFileOperations.java
File metadata and controls
74 lines (62 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.example;
import Model.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.io.File;
import java.io.IOException;
public class FileOperations {
public void fewMore() {
System.out.println("Hello, Vulnerable World!");
processFileName(" myFile.txt ");
useStringUtils(" test ");
fileUploadExample();
useJackson();
useSpring();
}
public static void processFileName(String name) {
System.out.println("Processing file: '" + StringUtils.strip(name) + "'");
}
public static void useStringUtils(String text) {
System.out.println("Stripped string: '" + StringUtils.strip(text) + "'");
}
public static void fileUploadExample() {
DiskFileItemFactory factory = new DiskFileItemFactory();
File tempDir = new File(System.getProperty("java.io.tmpdir"));
factory.setRepository(tempDir);
System.out.println("File upload factory created.");
}
// public static void useJackson() {
// ObjectMapper mapper = new ObjectMapper();
// User user = new User("Alice", 30);
// try {
// String json = mapper.writeValueAsString(user);
// System.out.println("User as JSON: " + json);
// String reversedName = new StringBuilder(user.name).reverse().toString();
// User user2 = mapper.readValue("{\"name\":\"" + reversedName + "\",\"age\":35}", User.class);
// System.out.println("User 2: " + user2);
// } catch (IOException e) {
// System.err.println("Error processing JSON: " + e.getMessage());
// }
// }
public static void useJackson() {
ObjectMapper mapper = new ObjectMapper();
UserWithIgnore user = new UserWithIgnore("Bob", 25, "secret");
try {
String json = mapper.writeValueAsString(user);
System.out.println("User with ignore as JSON: " + json);
// Attempting to deserialize might also reveal changes
UserWithIgnore user2 = mapper.readValue("{\"name\":\"Charlie\",\"age\":40,\"secret\":\"top\"}", UserWithIgnore.class);
System.out.println("User with ignore 2: " + user2);
} catch (IOException e) {
System.err.println("Error processing JSON: " + e.getMessage());
}
}
public static void useSpring() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
ObjectMapper springObjectMapper = builder.build();
System.out.println("Spring's ObjectMapper created: " + springObjectMapper.getClass().getName());
}
}