本地文件上传,下载和显示
Controller层
@RequestMapping("/uploadFile")
@ResponseBody
public Map<String, Object> uploadFile(MultipartFile mf) {
String oldName = mf.getOriginalFilename();
String newName = AppFileUtils.createNewFileName(oldName);
String dirName = DateUtil.format(new Date(), "yyyy-MM-dd");
File dirFile = new File(AppFileUtils.UPLOAD_PATH, dirName);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
File file=new File(dirFile, newName+"_temp");
try {
mf.transferTo(file);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
Map<String,Object> map=new HashMap<String, Object>();
map.put("path", dirName+"/"+newName+"_temp");
return map;
}
@RequestMapping("/showImage")
public ResponseEntity<Object> showImage(String path){
return AppFileUtils.createResponseEntity(path);
}
工具类
public class AppFileUtils {
public static String UPLOAD_PATH = "E:/upload/";
static {
String property = "E:/upload/";
if (null != property) {
UPLOAD_PATH = property;
}
}
public static String createNewFileName(String oldName) {
String stuff = oldName.substring(oldName.lastIndexOf("."), oldName.length());
return IdUtil.simpleUUID().toUpperCase() + stuff;
}
public static ResponseEntity<Object> createResponseEntity(String path) {
File file = new File(UPLOAD_PATH, path);
if (file.exists()) {
byte[] bytes = null;
try {
bytes = FileUtil.readBytes(file);
} catch (Exception e) {
e.printStackTrace();
}
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ResponseEntity<Object> entity = new ResponseEntity<>(bytes, header, HttpStatus.CREATED);
return entity;
}
return null;
}
public static String rename(String img) {
File file=new File(UPLOAD_PATH, img);
String replace = img.replace("_temp", "");
if (file.exists()){
file.renameTo(new File(UPLOAD_PATH,replace));
}
return replace;
}
public static void removeFileByPath(String img) {
if (!img.equals("images/timg.jpg")){
File file=new File(UPLOAD_PATH, img);
if (file.exists()){
file.delete();
}
}
}
}
图形验证码
Controller层
@Controller
@RequestMapping("/verifyCode")
public class CodeController {
@RequestMapping(value = "/getImg", method = {RequestMethod.POST, RequestMethod.GET})
protected void createImg(HttpServletRequest req, HttpServletResponse res) throws IOException {
Object[] objs = VerifyCodeUtil.createImage();
String imgcode = (String) objs[0];
HttpSession session = req.getSession();
session.setAttribute("imgcode", imgcode);
BufferedImage img = (BufferedImage) objs[1];
res.setContentType("image/png");
OutputStream os = res.getOutputStream();
ImageIO.write(img, "png", os);
os.close();
}
}
工具类
public final class VerifyCodeUtil {
private static final char[] chars = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
private static final int SIZE = 4;
private static final int LINES = 10;
private static final int WIDTH = 100;
private static final int HEIGHT = 32;
private static final int FONT_SIZE = 20;
public static Object[] createImage() {
StringBuffer sb = new StringBuffer();
BufferedImage image = new BufferedImage(
WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics graphic = image.getGraphics();
graphic.setColor(Color.LIGHT_GRAY);
graphic.fillRect(0, 0, WIDTH, HEIGHT);
Random ran = new Random();
for (int i = 0; i <SIZE; i++) {
int n = ran.nextInt(chars.length);
graphic.setColor(getRandomColor());
graphic.setFont(new Font(
null, Font.BOLD + Font.ITALIC, FONT_SIZE));
graphic.drawString(
chars[n] + "", i * WIDTH / SIZE, HEIGHT / 2);
sb.append(chars[n]);
}
for (int i = 0; i < LINES; i++) {
graphic.setColor(getRandomColor());
graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
}
return new Object[]{sb.toString(), image};
}
public static Color getRandomColor() {
Random ran = new Random();
Color color = new Color(ran.nextInt(256),
ran.nextInt(256), ran.nextInt(256));
return color;
}
密码加密
工具类
public class MD5Util {
public static String MD5(String inStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
public static String KL(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}
public static String JM(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 't');
}
String k = new String(a);
return k;
}
}