首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

求教?关于上传的解决方法

2013-10-27 
求教???关于上传的package cn.itcast.web.servletimport java.io.Fileimport java.io.FileOutputStream

求教???关于上传的


package cn.itcast.web.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet1 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*//request.getParameter("username");//***错误
request.setCharacterEncoding("UTF-8");//表单为上传文件,设置request 编码无效
*/
//得到上传文件的保存目录
try {
String savePath=this.getServletContext().getRealPath("/WEB-INF/upload");
System.out.println("文件上传的保存路径为:" + savePath);

DiskFileItemFactory factory=new DiskFileItemFactory();
factory.setRepository(new File(this.getServletContext().getRealPath("/WEB-INF/temp")));//设置临时文件路径

ServletFileUpload upload=new ServletFileUpload(factory);


upload.setHeaderEncoding("utf-8");//解决上传文件名的中文乱码问题
if(!upload.isMultipartContent(request)){
//按照传统方式获取数据
return;
}

List<FileItem> list=upload.parseRequest(request);
for(FileItem item:list){
if(item.isFormField()){
//fileitem中封装的是普通输入项的数据
String name=item.getFieldName();
System.out.println("字段名是:" + name);
String value=item.getString("utf-8");
System.out.println("字段值是:" + value);
}else{
String filename=item.getName();//不同的浏览器提交的文件是不一样的 c:\a\b\1.txt 1.txt
System.out.println(filename);
if(filename==null|| filename.trim().equals("")){
continue;//进行下一次循环
}
filename=filename.substring(filename.lastIndexOf("\")+1);//获取文件名称
String saveFilename=makeFileName(filename);//设定保存的文件名称

InputStream in=item.getInputStream();
String realSavePath=makePath(saveFilename,savePath);//得到文件保存的目录
FileOutputStream out=new FileOutputStream(realSavePath + "\" + saveFilename);
byte buffer[]=new byte[1024];
int len=0;
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
out.close();
item.delete();//删除临时文件

}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}



private String makePath(String saveFilename, String savePath) {
int hashcode=saveFilename.hashCode();
int dir1=hashcode&0xf;//0--15 取0-3位二进制位
int dir2=(hashcode&0xf0)>>4;//0--15 取4--7位二进制位

String dir=savePath + "\" + dir1 + "\" +dir2;
File file=new File(dir);
if(!file.exists()){
file.mkdirs();
}
return dir;
}

private String makeFileName(String filename) {
return UUID.randomUUID().toString() + "_" + filename;
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);

}

}



错误提示:

文件上传的保存路径为:E:\apache-tomcat-6.0.20\webapps\myday20\WEB-INF\upload
org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. E:\apache-tomcat-6.0.20\webapps\myday20\WEB-INF\temp\upload_6c086b6_141f3c6e3b1__8000_00000001.tmp (系统找不到指定的路径。)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at cn.itcast.web.servlet.UploadServlet1.doGet(UploadServlet1.java:44)
at cn.itcast.web.servlet.UploadServlet1.doPost(UploadServlet1.java:103)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)


at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: E:\apache-tomcat-6.0.20\webapps\myday20\WEB-INF\temp\upload_6c086b6_141f3c6e3b1__8000_00000001.tmp (系统找不到指定的路径。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.commons.io.output.DeferredFileOutputStream.thresholdReached(DeferredFileOutputStream.java:165)
at org.apache.commons.io.output.ThresholdingOutputStream.checkThreshold(ThresholdingOutputStream.java:221)
at org.apache.commons.io.output.ThresholdingOutputStream.write(ThresholdingOutputStream.java:127)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:101)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:362)
... 17 more
java
[解决办法]
真实文件名你应该没有得到,你得到的是临时文件名

热点排行