@Override
public String execute() throws Exception {
InputStream is = new FileInputStream(getFile());
String root = ServletActionContext.getRequest().getRealPath("/upload");
System.out.println("========="+root);
File destFile = new File(root, this.getFileFileName());
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[50];
int length = 0;
while ((length - is.read(buffer)) > 0){
os.write(buffer, 0, length);
}
is.close();
os.close();
return "result";
}我每次上传的文件或者图片字节都是0,打不开,那个大哥大姐帮我看一下。。纠正一下错误,或者给一段可以用的上传代码给我。。谢谢了……………………在线等哦!!!
解决方案 »
- hibernate+struts无法获取session???新手,第一次发帖,求细致、求温柔?
- 这个错怎么办
- hibernate mysql 日文
- NETBEANS下开发Visual Web JSF
- 很急的Hibernate问题。。。。
- 高手请教:股市走势图怎么做!
- 关于beanutils.copyproperties的问题
- struts里,一action的操作时间比较长,如果这个时候用户刷新......
- eclipse 3.0中的tomcat插件在哪里?
- java连接DB2的错误,实在搞不定了。??????????????QQQ
- 关于jsp时间问题,麻烦各位了~~
- Hibernate 分页sql语句问题
while ((length = is.read(buffer)) > 0){
<s:form action="upload" enctype="multipart/form-data">
<s:textfield name="username" id="username" label="username"/>
<s:file name="file" id="file" label="file"/>
<s:submit/>
</s:form>
我后台是这样写的
@Override
public String execute() throws Exception {
InputStream is = new FileInputStream(getFileName());
String root = ServletActionContext.getRequest().getRealPath("/upload");
System.out.println("========="+root);
File destFile = new File(root,this.getFileName());
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[50];
int length = 0;
while ((length = is.read(buffer)) > 0){
os.write(buffer, 0, length);
}
is.close();
os.close();
return "result";
} 现在报错java.io.FileNotFoundException: E:\hub\huborcl\WebRoot\upload\tmp\upload_3393dfb8_129b61967e2__8000_00000003.tmp (系统找不到指定的路径。)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.<init>(FileOutputStream.java:179)
java.io.FileOutputStream.<init>(FileOutputStream.java:131)
cn.edu.hust.document.action.DocumentAction.execute(DocumentAction.java:89)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
这个文件夹有没有?没有自己建一下
这个肯定是这么改的,如果是while ((length - is.read(buffer)) > 0)这样,肯定是有问题的,你现在的问题是路径不对,E:\hub\huborcl\WebRoot\upload这个路径找不到,手动创建这个目录,再试。
<s:file name="upload" label="文件:"></s:file><br />
<s:file name="upload" label="文件:"></s:file><br />
<s:file name="upload" label="文件:"></s:file><br />
<s:submit value="确定"></s:submit>
</s:form>package com.learn.web.action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;import com.opensymphony.xwork2.Action;public class FileUploadAction implements Action { private File[] upload;
private String[] uploadFileName;
private String[] uploadContentType;
private String allowFiles; public String execute() throws Exception { System.out.println("uploadFileName: " + uploadFileName + " uploadContentType: " + uploadContentType);
for(int i = 0;i < upload.length;i++) {
FileOutputStream fos = new FileOutputStream("C:/" + uploadFileName[i]);
FileInputStream fis = new FileInputStream(upload[i]);
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) > 0)
fos.write(buffer, 0, len);
}
return SUCCESS;
} public File[] getUpload() {
return upload;
} public void setUpload(File[] upload) {
this.upload = upload;
} public String[] getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
} public String[] getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
} public String getAllowFiles() {
return allowFiles;
} public void setAllowFiles(String allowFiles) {
this.allowFiles = allowFiles;
}
}