fileupload组件文件上传的疑难问题紧急求助
我用fileupload组件做一个上传功能的jsp页面。
文件可以上传。
现在问题是。如果上传的是txt文件,没有问题
如果是xls,doc…bmp,jpg…等带有格式的文件,则在上传后的目录不能完整的打开,提示信息丢失,或者文件遭到损害
如果是图片,颜色会发生变化。。
网上搜了一下,fileupload组件好像别人没有遇到这个问题。
网友用我的代码运行,也正常。
是我哪个地方没配置好?还是怎么回事?
form等均已检查过~~
急阿!!!!!!!!!
大虾帮忙。代码如下:
*******************************************
try
{
String tempFilepath = "E:\\mes\\jacMes\\upload\\";
String savepath="E:\\mes\\jacMes\\upload\\assemble\\";
//如果没有临时目录,则创建它
if(!(new File(tempFilepath).isDirectory())){
try{
new File(tempFilepath).mkdirs();
}catch(SecurityException e){
System.out.println("can not make security direcoty");
}
}
//如果没有上传目的目录,则创建它
if(!(new File(savepath).isDirectory())){
try{
new File(savepath).mkdirs();
}catch(SecurityException e){
System.out.println("can not make security direcoty");
}
}
DiskFileUpload upload =new DiskFileUpload ();
upload.setSizeThreshold(4096000); //最大允许缓存40K的文件
upload.setSizeMax(2621440); //最大允许上传2.5M的文件
upload.setRepositoryPath(tempFilepath);
String s =DiskFileUpload.FORM_DATA;
System.out.println("__________________________"+s);
List uploadlist= upload.parseRequest(req);
Iterator iter=uploadlist.iterator();
while(iter.hasNext())
{
FileItem item=(FileItem)iter.next();
if(!item.isFormField())
{
String filename=item.getName();
filename = filename.substring(filename.lastIndexOf("\\")+1);//从全路径中提取文件名
// long size = item.getSize();
if((filename == null) ¦ ¦ filename.equals("") && size == 0)
continue;
savepath= savepath+filename;
System.out.println("savepath is "+savepath);
File saveFilepath = new File(savepath);
item.write(saveFilepath);
}
}
}catch(Exception ex)
{
ex.printStackTrace();
System.out.println("程序发生错误,抛出异常为 "+ex.getMessage());
}
}
jsp页面代码如下:
************************************************
<form method="post" id="upform" name = "upform" action="action.UploadOfAssembleAction.do" ENCTYPE="multipart/form-data">
<table width="100%" border="1" >
<tr>
<td width="55%">
<input type="file" name="fileone" size="60"/>
</td>
<td width="45%">
<input id="submit" name = "submit" type="submit" value="上传" /><input type="reset" value="取消"/>
</td>
</tr>
</table>
</form>
我用fileupload组件做一个上传功能的jsp页面。
文件可以上传。
现在问题是。如果上传的是txt文件,没有问题
如果是xls,doc…bmp,jpg…等带有格式的文件,则在上传后的目录不能完整的打开,提示信息丢失,或者文件遭到损害
如果是图片,颜色会发生变化。。
网上搜了一下,fileupload组件好像别人没有遇到这个问题。
网友用我的代码运行,也正常。
是我哪个地方没配置好?还是怎么回事?
form等均已检查过~~
急阿!!!!!!!!!
大虾帮忙。代码如下:
*******************************************
try
{
String tempFilepath = "E:\\mes\\jacMes\\upload\\";
String savepath="E:\\mes\\jacMes\\upload\\assemble\\";
//如果没有临时目录,则创建它
if(!(new File(tempFilepath).isDirectory())){
try{
new File(tempFilepath).mkdirs();
}catch(SecurityException e){
System.out.println("can not make security direcoty");
}
}
//如果没有上传目的目录,则创建它
if(!(new File(savepath).isDirectory())){
try{
new File(savepath).mkdirs();
}catch(SecurityException e){
System.out.println("can not make security direcoty");
}
}
DiskFileUpload upload =new DiskFileUpload ();
upload.setSizeThreshold(4096000); //最大允许缓存40K的文件
upload.setSizeMax(2621440); //最大允许上传2.5M的文件
upload.setRepositoryPath(tempFilepath);
String s =DiskFileUpload.FORM_DATA;
System.out.println("__________________________"+s);
List uploadlist= upload.parseRequest(req);
Iterator iter=uploadlist.iterator();
while(iter.hasNext())
{
FileItem item=(FileItem)iter.next();
if(!item.isFormField())
{
String filename=item.getName();
filename = filename.substring(filename.lastIndexOf("\\")+1);//从全路径中提取文件名
// long size = item.getSize();
if((filename == null) ¦ ¦ filename.equals("") && size == 0)
continue;
savepath= savepath+filename;
System.out.println("savepath is "+savepath);
File saveFilepath = new File(savepath);
item.write(saveFilepath);
}
}
}catch(Exception ex)
{
ex.printStackTrace();
System.out.println("程序发生错误,抛出异常为 "+ex.getMessage());
}
}
jsp页面代码如下:
************************************************
<form method="post" id="upform" name = "upform" action="action.UploadOfAssembleAction.do" ENCTYPE="multipart/form-data">
<table width="100%" border="1" >
<tr>
<td width="55%">
<input type="file" name="fileone" size="60"/>
</td>
<td width="45%">
<input id="submit" name = "submit" type="submit" value="上传" /><input type="reset" value="取消"/>
</td>
</tr>
</table>
</form>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货