jsp 上传图片问题

huiyang1985 2009-12-31 10:30:43
谁有源码啊?能否传下啊?谢谢了!
...全文
69 7 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
whfl224 2010-01-06
  • 打赏
  • 举报
回复
还是好人多啊
huiyang1985 2010-01-06
  • 打赏
  • 举报
回复
谢谢,但是我不是框架做的,我不是用这个组件做的 severlet和jsp的 ,急。。。
boboo_2000_0 2010-01-06
  • 打赏
  • 举报
回复
下面是Struts多文件上传代码:
jsp页面:

<html:form action="/upload.do?action=upload" method="post" enctype="multipart/form-data">
<html:file property="file1"/><br>
<html:file property="file2"/><br>
<html:file property="file3"/><br>
<html:file property="file4"/><br>
<html:file property="file5"/><br>
<html:submit>上传</html:submit>
</html:form>

Struts的Action:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.upload.CommonsMultipartRequestHandler;
import org.apache.struts.plugins.ModuleConfigVerifier;

import com.sunfairs.exception.FileUnloadException;
import com.sunfairs.util.FileUnloadUtil;

/**
* MyEclipse Struts
* Creation date: 07-08-2009
*
* XDoclet definition:
* @struts.action parameter="action" validate="true"
*/
public class UploadAction extends DispatchAction {
private static final Log log = LogFactory.getLog(UploadAction.class);
public static final String UPLOAD_FOLDER = "/upload";

public ActionForward upload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MultipartRequestHandler handler = form.getMultipartRequestHandler();
Hashtable elements = handler.getFileElements();
String uploadPath = request.getRealPath(UPLOAD_FOLDER);
File path = new File(uploadPath);
if(!path.exists()){
path.mkdirs();
}

List<FileUnloadUtil.FileProperty> propertyList = new ArrayList<FileUnloadUtil.FileProperty>();
Collection values = elements.values();
Iterator it = values.iterator();
FileOutputStream output = null;
while(it.hasNext()){
FormFile file = (FormFile) it.next();
FileUnloadUtil.FileProperty property = new FileUnloadUtil.FileProperty();
String fileName = file.getFileName();
String contentType = file.getContentType();
int size = file.getFileSize();
int dotPosition = fileName.lastIndexOf(".");
String suffix = fileName.substring(dotPosition+1, fileName.length());
String fileType = "";
String prefixName = "";
String randName = "";
String uploadPaths = "";
String uploadName = "";
Map map = new HashMap();
try {
map = FileUnloadUtil.getFileType(fileName);
fileType =(String) map.get("fileType");
prefixName = (String) map.get("prefixName");
randName = FileUnloadUtil.getRandomName();
uploadPaths = uploadPath+File.separator+prefixName+randName+"."+suffix;
uploadName = request.getContextPath()+UPLOAD_FOLDER+"/"+prefixName+randName+"."+suffix;
property.setName(uploadName);
property.setSize((float)size/1024);
property.setType(fileType);
property.setContentType(contentType);
property.setUploadTime(new Date());
propertyList.add(property);
} catch (FileUnloadException e1) {
property.setName("未产生文件");
property.setSize(0.0f);
property.setType("未知");
property.setContentType(contentType);
property.setUploadTime(new Date());
propertyList.add(property);
continue;
}
if(file!=null && !fileName.equals("") && size!=0){
byte[]data = new byte[file.getFileSize()];
try {
output = new FileOutputStream(uploadPaths);
data = file.getFileData();
output.write(data);
output.flush();
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
request.setAttribute("propertyList", propertyList);
return mapping.findForward("uploads");
}
}

struts-config配置:

<form-bean name="uploadForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="file1" type="java.lang.String"/>
<form-property name="file2" type="java.lang.String"/>
<form-property name="file3" type="java.lang.String"/>
<form-property name="file4" type="java.lang.String"/>
<form-property name="file5" type="java.lang.String"/>
</form-bean>
<action
attribute="uploadForm"
name="uploadForm"
parameter="action"
path="/upload"
type="com.sunfairs.struts.action.UploadAction">
<forward name="uploads" path="/uploads.jsp"/>
</action>
zhangyy130 2010-01-04
  • 打赏
  • 举报
回复
客户端可以直接用 <input type="file">
服务端需要servlet配合,代码贴在这里不好看。
满地都有上传的东东。
fb1zwb 2010-01-01
  • 打赏
  • 举报
回复
利用一些组件可以很方便的实现文件上传 如commons-fileupload-1.2.1.jar
skfeng36 2009-12-31
  • 打赏
  • 举报
回复
是将图片2进制的形式放到数据库中?
yzq513164922 2009-12-31
  • 打赏
  • 举报
回复
我有,把40分给我我就给你
相关推荐
发帖
Java

4.9w+

社区成员

Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
帖子事件
创建了帖子
2009-12-31 10:30
社区公告
暂无公告