Skip to content

Instantly share code, notes, and snippets.

@lzxz1234
Last active June 8, 2017 02:21
Show Gist options
  • Save lzxz1234/f6253ae3a0f79ed33061 to your computer and use it in GitHub Desktop.
Save lzxz1234/f6253ae3a0f79ed33061 to your computer and use it in GitHub Desktop.
XCMS 自处理图标用DAO
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.nutz.lang.Mirror;
import org.nutz.lang.Strings;
import org.nutz.mvc.upload.TempFile;
import com.chineseall.xcms.core.utils.Ctx;
import com.chineseall.xcms.nb.dao.NutzDao;
import com.chn.xietong.cms.util.FilePool;
public class AutoSolveFileDao extends NutzDao {
@Override
public void add(Object target) {
HttpServletRequest request = Ctx.get(HttpServletRequest.class, "request");
Enumeration<String> paramNames = request.getParameterNames();
String paramName;
while(paramNames.hasMoreElements()) {
paramName = paramNames.nextElement();
if(paramName.endsWith("InSession"))
autoUpload(target, request, paramName);
}
super.add(target);
}
@Override
public void mod(Object target) {
HttpServletRequest request = Ctx.get(HttpServletRequest.class, "request");
Enumeration<String> paramNames = request.getParameterNames();
String paramName;
while(paramNames.hasMoreElements()) {
paramName = paramNames.nextElement();
if(paramName.endsWith("InSession"))
autoUpload(target, request, paramName);
}
super.mod(target);
}
private void autoUpload(Object target, HttpServletRequest request,
String paramName) {
try {
String fieldName = paramName.substring(0, paramName.indexOf("InSession"));
String inSessionName = request.getParameter(paramName);
HttpSession session = request.getSession();
TempFile tempFile = (TempFile) session.getAttribute(inSessionName);
byte[] fileContent = FileUtils.readFileToByteArray(tempFile.getFile());
String fileName = tempFile.getMeta().getFileLocalName();
String reqUrl = FilePool.upload(fileContent, fileName);
Mirror.me(this.getReq().getEntityClass()).invoke(target, "set" + Strings.capitalize(fieldName), reqUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment