文件上传时 怎么获取一般参数值
1个回答
2014-11-17
展开全部
[align=right][align=left]DiskFileItemFactory factory=new DiskFileItemFactory();
String path=request.getRealPath("/upload");
System.out.println(path);
factory.setRepository(new File(path));
factory.setSizeThreshold(1024*1024);
ServletFileUpload upload=new ServletFileUpload(factory);
try{
List<FileItem> ls=upload.parseRequest(request);
for (FileItem item : ls) {
if(item.isFormField()){
String name=item.getFieldName();
String value=item.getString("gbk");
request.setAttribute(name, value);
}else{
String name=item.getFieldName();
String value=item.getName();
int start=value.lastIndexOf("\\");
String fileName=value.substring(start+1);
System.out.println(fileName);
request.setAttribute(name, fileName);
Encoding en=new Encoding();
//item.write(new File(path,fileName));
OutputStream os=new FileOutputStream(new File(path,fileName));
InputStream is=item.getInputStream();
byte [] bt=new byte[400];
int length=0;
while((length=is.read(bt))>0){
os.write(bt, 0, length);
}
os.close();
is.close();
}
}
}catch (Exception e) {
e.printStackTrace();
}[/align][/align]
String path=request.getRealPath("/upload");
System.out.println(path);
factory.setRepository(new File(path));
factory.setSizeThreshold(1024*1024);
ServletFileUpload upload=new ServletFileUpload(factory);
try{
List<FileItem> ls=upload.parseRequest(request);
for (FileItem item : ls) {
if(item.isFormField()){
String name=item.getFieldName();
String value=item.getString("gbk");
request.setAttribute(name, value);
}else{
String name=item.getFieldName();
String value=item.getName();
int start=value.lastIndexOf("\\");
String fileName=value.substring(start+1);
System.out.println(fileName);
request.setAttribute(name, fileName);
Encoding en=new Encoding();
//item.write(new File(path,fileName));
OutputStream os=new FileOutputStream(new File(path,fileName));
InputStream is=item.getInputStream();
byte [] bt=new byte[400];
int length=0;
while((length=is.read(bt))>0){
os.write(bt, 0, length);
}
os.close();
is.close();
}
}
}catch (Exception e) {
e.printStackTrace();
}[/align][/align]
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询