BaseController.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package com.mes.controller.barcode;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URLEncoder;
  4. import org.springframework.http.HttpHeaders;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.ui.ModelMap;
  9. public class BaseController
  10. {
  11. protected static final String defultCharset = "UTF-8";
  12. private static final String SUCCESS = "success";
  13. private static final String ERROR = "error";
  14. private static final String ERROR_CODE = "errCode";
  15. private static final String CONTENT = "content";
  16. private static final String ERROR_MESSAGE = "exceptionInfo";
  17. protected String decodeUnicodeString(String param)
  18. {
  19. try
  20. {
  21. return new String(param.getBytes("utf-8"), "UTF-8"); } catch (UnsupportedEncodingException e) {
  22. }
  23. return param;
  24. }
  25. protected ModelMap success()
  26. {
  27. return new ModelMap("success", Boolean.valueOf(true));
  28. }
  29. protected ModelMap success(Object content) {
  30. return success().addAttribute("content", content);
  31. }
  32. protected ModelMap error() {
  33. return new ModelMap("error", Boolean.valueOf(true));
  34. }
  35. protected ModelMap error(String errMsg) {
  36. return error().addAttribute("exceptionInfo", errMsg);
  37. }
  38. protected ModelMap error(int errCode, String errMsg) {
  39. return error(errMsg).addAttribute("errCode", Integer.valueOf(errCode));
  40. }
  41. protected ResponseEntity<byte[]> outputStream(String fileName, byte[] bytes)
  42. {
  43. HttpHeaders headers = new HttpHeaders();
  44. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  45. try {
  46. headers.setContentDispositionFormData("attachment", URLEncoder.encode(fileName, "UTF-8"));
  47. } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
  48. }
  49. return new ResponseEntity(bytes, headers, HttpStatus.CREATED);
  50. }
  51. }