当前位置:编程学习 > JAVA >>

大家来帮我看看这两个代码的问题吧~谢谢了

package com.fzu.fileuploadservlet;

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

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
@Override
public String execute() throws Exception {
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
try {
String realPath=ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
String tempPath=ServletActionContext.getServletContext().getRealPath("/WEB-INF/temp");
File tempFile=new File(tempPath);
//设置内存缓冲区的大小,默认值为10K
//指定临时文件目录,默认值为System.getProperty("java.io.tmpdir").
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
diskFileItemFactory.setRepository(tempFile);
ServletFileUpload fileUpload=new ServletFileUpload(diskFileItemFactory);
fileUpload.setFileSizeMax(1024*1024*1024);//设置单个上传文件不能超过1G,单位:字节
fileUpload.setSizeMax(10*1024*1024*1024);//设置上传总文件大小不能超过10G 单位:字节
fileUpload.setHeaderEncoding("utf-8");
//判断表单是否enctype="multipart/form-data"
boolean isMultipartContent=fileUpload.isMultipartContent(request);
if(isMultipartContent)
{
List<FileItem> fileList=fileUpload.parseRequest(request);
for(FileItem item:fileList)
{
if(item.isFormField())
{
//表示只是普通的表单信息,直接打印出来
System.out.println(item.getString("utf-8"));
}else{
//表示上传的是文件
String fileName=item.getName();
if(fileName!=null&&!"".equals(fileName))
{
InputStream in=item.getInputStream();
int len=-1;

String filePath=makeFilePath(realPath,fileName);
// System.out.println(filePath);

filePath=filePath+"\\"+UUID.randomUUID().toString()+"_"+fileName;
FileOutputStream outputStream=new FileOutputStream(filePath);
byte[] b=new byte[1024];
while((len=in.read(b))!=-1)
{
outputStream.write(b, 0, len);
}
in.close();
outputStream.close();
item.delete();//删除临时文件
}
}
}
}else{
return INPUT;
}
}catch(FileUploadBase.FileSizeLimitExceededException e1){
request.setAttribute("message", "上传的单个文件大小不能超过1G");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}catch(FileUploadBase.SizeLimitExceededException e2){
request.setAttribute("message", "上传的总文件大小不能超过10G");
request.getRequestDispatcher("/message.jsp").forward(request, response);

}
catch (Exception e) {
throw new RuntimeException(e);
}
return SUCCESS;
}
private String makeFilePath(String realPath, String fileName) {
String filePath="";
String firstDir=(fileName.hashCode()&0x0f)+"";
String secondDir=(fileName.hashCode()&0xf0)+"";
filePath=realPath+"\\"+firstDir+"\\"+secondDir;
File file=new File(filePath);
if(!file.exists())
file.mkdirs();
System.out.println(filePath);
return filePath;
}
}
**********************************我是分割线*********************************************

package com.fzu.fileuploadservlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
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.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadServlet extends HttpServlet {
private static Properties properties=new Properties();
static{
try {
properties.load(FileUploadServlet.class.getClassLoader().getResourceAsStream("file.cfg.properties"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String realPath=getServletContext().getRealPath(properties.getProperty("uploadPath"));
String tempPath=getServletContext().getRealPath(properties.getProperty("tempPath"));
File tempFile=new File(tempPath);
//设置内存缓冲区的大小,默认值为10K
//指定临时文件目录,默认值为System.getProperty("java.io.tmpdir").
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
diskFileItemFactory.setRepository(tempFile);
ServletFileUpload fileUpload=new ServletFileUpload(diskFileItemFactory);
fileUpload.setFileSizeMax(Integer.parseInt(properties.getProperty("fileSizeMax")));//设置单个上传文件不能超过1G,单位:字节
fileUpload.setSizeMax(Integer.parseInt(properties.getProperty("sizeMax")));//设置上传总文件大小不能超过10G 单位:字节
fileUpload.setHeaderEncoding("utf-8");
//判断表单是否enctype="multipart/form-data"
boolean isMultipartContent=fileUpload.isMultipartContent(request);
if(isMultipartContent)
{
List<FileItem> fileList=fileUpload.parseRequest(request);
for(FileItem item:fileList)
{
if(item.isFormField())
{
//表示只是普通的表单信息,直接打印出来
System.out.println(item.getString("utf-8"));
}else{
//表示上传的是文件
String fileName=item.getName();
if(fileName!=null&&!"".equals(fileName))
{
InputStream in=item.getInputStream();
int len=-1;

String filePath=makeFilePath(realPath,fileName);
// System.out.println(filePath);

filePath=filePath+"\\"+UUID.randomUUID().toString()+"_"+fileName;
FileOutputStream outputStream=new FileOutputStream(filePath);
byte[] b=new byte[1024];
while((len=in.read(b))!=-1)
{
outputStream.write(b, 0, len);
}
in.close();
outputStream.close();
item.delete();//删除临时文件
}
}
}
}else{
return ;
}
}catch(FileUploadBase.FileSizeLimitExceededException e1){
request.setAttribute("message", properties.getProperty("message1"));
request.getRequestDispatcher("/message.jsp").forward(request, response);
}catch(FileUploadBase.SizeLimitExceededException e2){
request.setAttribute("message", properties.getProperty("message2"));
request.getRequestDispatcher("/message.jsp").forward(request, response);

}
catch (Exception e) {
throw new RuntimeException(e);
}

}

private String makeFilePath(String realPath, String fileName) {
String filePath="";
String firstDir=(fileName.hashCode()&0x0f)+"";
String secondDir=(fileName.hashCode()&0xf0)+"";
filePath=realPath+"\\"+firstDir+"\\"+secondDir;
File file=new File(filePath);
if(!file.exists())
file.mkdirs();
System.out.println(filePath);
return filePath;



}

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

}
**************************我是分割线*****************************
servlet那个可以成功上传文件  可是  action那个不行啊  这是什么情况呢
struts2什么的也配置正确。。虽说struts2中的request封装过  可是这些方法应该都还在都还可以用的呀   两个一模一样的代码怎么就不行了     请高手解答  谢谢了!! struts
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,