我用struts2+uploadify上传时,为什么后台得到我文件类型全部都是application/octet-stream

Pbulic 2011-11-16 04:24:16
struts.xml
<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();
}
}


...全文
977 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
力力尼 2013-06-03
  • 打赏
  • 举报
回复
这个问题,谁解决了?我也遇到这个问题。。。。
bbqmdot 2012-08-20
  • 打赏
  • 举报
回复
同问这个问题,楼主如何解决的?
Pbulic 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ruogannian 的回复:]
<form name="form" method="post" action="" enctype="multipart/form-data">
[/Quote]
是用JQUERY uploadify 上传的。
若干年 2011-11-16
  • 打赏
  • 举报
回复
<form name="form" method="post" action="" enctype="multipart/form-data">

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧