struts2 上传文件 File 为 Null 不走set方法
后缀改为了html<s:formaction="uploadimage.html"method="post"enctype="multipart/form-data"><...
后缀改为了html
<s:form action="uploadimage.html" method="post" enctype="multipart/form-data">
<s:file name="upFile"></s:file>
<s:submit label="上传" />
</s:form>
private File upFile;
private String upFileFileName;
private String upFileContentType;
public String execute() throws Exception {
String path = ServletActionContext.getServletContext().getRealPath("/uploadFile/");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
bis = new BufferedInputStream(new FileInputStream(upFile));
bos = new BufferedOutputStream(new FileOutputStream(new File(dir,upFileFileName)));
byte[] buf = new byte[(int)upFile.length()];
int len = 0;
while(((len=bis.read(buf))!=-1)){
bos.write(buf, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
}catch(Exception e){
bos = null;
bis = null;
}
}
return SUCCESS;
}
public File getUpFile() {
return upFile;
}
public void setUpFile(File upFile) {
this.upFile = upFile;
}
public String getUpFileFileName() {
return upFileFileName;
}
public void setUpFileFileName(String upFileFileName) {
this.upFileFileName = upFileFileName;
}
public String getUpFileContentType() {
return upFileContentType;
}
public void setUpFileContentType(String upFileContentType) {
this.upFileContentType = upFileContentType;
} 展开
<s:form action="uploadimage.html" method="post" enctype="multipart/form-data">
<s:file name="upFile"></s:file>
<s:submit label="上传" />
</s:form>
private File upFile;
private String upFileFileName;
private String upFileContentType;
public String execute() throws Exception {
String path = ServletActionContext.getServletContext().getRealPath("/uploadFile/");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
bis = new BufferedInputStream(new FileInputStream(upFile));
bos = new BufferedOutputStream(new FileOutputStream(new File(dir,upFileFileName)));
byte[] buf = new byte[(int)upFile.length()];
int len = 0;
while(((len=bis.read(buf))!=-1)){
bos.write(buf, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
}catch(Exception e){
bos = null;
bis = null;
}
}
return SUCCESS;
}
public File getUpFile() {
return upFile;
}
public void setUpFile(File upFile) {
this.upFile = upFile;
}
public String getUpFileFileName() {
return upFileFileName;
}
public void setUpFileFileName(String upFileFileName) {
this.upFileFileName = upFileFileName;
}
public String getUpFileContentType() {
return upFileContentType;
}
public void setUpFileContentType(String upFileContentType) {
this.upFileContentType = upFileContentType;
} 展开
展开全部
给我winwsk@qq.com发个邮件。我传给你这个代码。我看你这个没看清楚
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
null 怎么set呢?
JS判断下嘛~
JS判断下嘛~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
action="uploadimage.html"
---------------------------
你把action的后缀配置为html?
好好检查一下。
---------------------------
你把action的后缀配置为html?
好好检查一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询