Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e5cf6c

Browse files
Update README.md
1 parent d53bf2d commit 7e5cf6c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ HttpSession session = request.getSession();
6666
* [Custom error page](#error-page)
6767
* [Broken session Managment](#Session)
6868
* [Cross site Scripting](#Xss-Fix)
69+
* [Unrestricted file upload](#file-upload-secure)
6970

7071

7172
## Secure-headers
@@ -155,4 +156,42 @@ ${StringEscapeUtils.escapeHtml(obj.name)}
155156
}
156157
```
157158

159+
## file-upload-secure
160+
161+
```
162+
163+
public ModelAndView upload(String file) {
164+
String UPLOAD_FOLDER = "C://test//";
165+
166+
String fileExtentions = "jpg,jpeg,png";
167+
168+
String substring = FilenameUtils.getExtension(file);
169+
170+
System.out.println(substring);
171+
if (!fileExtentions.contains(substring) || substring.isEmpty())
172+
{
173+
System.out.println("sorry");
174+
return new ModelAndView("status", "message", " file type not supported");
175+
176+
}
177+
178+
else {
179+
int abcd=file.hashCode();
180+
System.out.println("good");
181+
String filter= String.valueOf(abcd).concat(".png");
182+
System.out.println(filter);
183+
byte[] bytes = file.getBytes();
184+
Path path = Paths.get(UPLOAD_FOLDER + filter);
185+
try {
186+
Files.write(path, bytes);
187+
} catch (IOException e) {
188+
// TODO Auto-generated catch block
189+
e.printStackTrace();
190+
}
191+
}
192+
//log
193+
return new ModelAndView("status", "message", "succes");
194+
195+
}
196+
```
158197

0 commit comments

Comments
 (0)