67,513
社区成员
发帖
与我相关
我的任务
分享
<action name="upload" class="com.cnspot.action.UploadAction" method="upload"></action>
private static final int BUFFER_SIZE = 122880;
private static final String FILE_TYPE = "image/jpeg,image/pjpeg";
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String imageMyFileName;
private String caption;
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String getImageMyFileName() {
return imageMyFileName;
}
public void setImageMyFileName(String imageMyFileName) {
this.imageMyFileName = imageMyFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
/**
* 替换文件上传中出现的错误信息
*/
/*
* @Override public void addActionError(String anErrorMessage) { //
* 这里要先判断一下,是我们要替换的错误,才处理 if
* (anErrorMessage.startsWith("the request was rejected because its size"))
* { // 这些只是将原信息中的文件大小提取出来。 Matcher m =
* Pattern.compile("\\d+").matcher(anErrorMessage); String s1 = ""; if
* (m.find()) { s1 = m.group(); } String s2 = ""; if (m.find()) { s2 =
* m.group();// 偷梁换柱,将信息替换掉 super.addActionError("你上传的文件(" + s1 +
* ")超过允许的大小(" + s2 + ")"); } else {// 不是则不管它
* super.addActionError(anErrorMessage); } } }
*/
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void upload() throws Exception {
response.setHeader("ContentType", "text/json");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
System.out.println(myFileFileName);
System.out.println(myFileContentType);//这里输出为:application/octet-stream System.out.println(myFile.length());
if (FILE_TYPE.indexOf(myFileContentType) == -1) {
pw.write("{\"error\":\"-1\"}");
pw.flush();
pw.close();
} else if (myFile.length() > 122880) {// 120K
pw.write("{\"error\":\"-2\"}");
pw.flush();
pw.close();
} else {
imageMyFileName = new Date().getTime()
+ uploadService.getExtention(myFileFileName).toLowerCase();
String uploadRoot = ServletActionContext.getServletContext()
.getRealPath("/temp");
System.out.println(uploadRoot);
String tempPath = uploadService.tempPath(uploadRoot);
ScaleImage is = new ScaleImage();
is.saveImageAsJpg(myFile.toString(), tempPath + "big"
+ imageMyFileName, 300, 300);
is.saveImageAsJpg(myFile.toString(), tempPath + "small"
+ imageMyFileName, 100, 100);
String imgPath = tempPath.replace(uploadRoot, "");
System.out.println(imgPath + "===");
pw.write("{\"msg\":\"/cnspot/temp" + imgPath + "small"
+ imageMyFileName + "\",\"error\":\"\"}");
pw.flush();
pw.close();
}
}