会员注册中有一个上传头像功能,会员远程注册时怎样获得上传头像的地址?
我用的就是通过表单获取地址,单用户注册时上传图片的时候:比如用户选择的头像是
C:\Users\hp\Desktop\zb_download.doc
就报错C:\Users\hp\Desktop\zb_download.doc (系统找不到指定的路径。),因为因为服务器上没有此文件,
怎样才能获得用户上传的那张图片呢?
我用的就是通过表单获取地址,单用户注册时上传图片的时候:比如用户选择的头像是
C:\Users\hp\Desktop\zb_download.doc
就报错C:\Users\hp\Desktop\zb_download.doc (系统找不到指定的路径。),因为因为服务器上没有此文件,
怎样才能获得用户上传的那张图片呢?
解决方案 »
- myeclipes java 调用 matlab 类包问题
- 请问如何处理这个字符串
- 新手问个问题.......进来看看?????
- 用JAVABEAN读取菜单数据库并生成客户端脚本,急啊!!!!
- 请问高手,在jsp 中打印,跟浏览器版本或者是操作系统有什么关系吗?
- 请问如何判断几个radio是否都没有被选中?
- jspsmart上传组件如何用?如何做着急.请大家帮帮我.
- 怎么知道ResultSet 的行数?
- ===========请教:如何实现Input(text类型)框中部分value的选中===========
- 最近遇到的一个Java Socket通信的小问题
- jsp页面传递%问题!
- JBPM任务分配失败问题
兄弟 你的思想不对
你想下 一个是服务器 一个是客户端
客户上传一个路径 然后服务器端在客户端 找这个文件
能找到嘛
就算能 如果图片名称 路径改了怎么办
删了呢?!所有一般是把图片上传到服务器端 然后重新命名
把这个 相对路径 以及 新名称放入服务器端
一下代码:
action:
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AddForm addForm = (AddForm) form; ActionErrors errors = new ActionErrors();
try {
// 获得要上传文件
FormFile file = addForm.getFile();
System.out.println(file.getContentType());
if (!"image/pjpeg".equals(file.getContentType())) {
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"你选择的文件类型有误", false));
this.addErrors(request, errors);
return mapping.findForward("error");
}
// 获得上传文件名
String filename = file.getFileName(); // 获得新的文件名称
String newfilename = DataDefine.getDateId()
+ filename.substring(filename.lastIndexOf("."));
//DataDefine.getDateId() 是得到当前系统时间的日期 时分秒
//创建一个新名称 这样名称不会重复 以便管理
String imagespath = "images" + "/" + newfilename; // 获得服务器上传目录
String dir = this.getServlet().getServletContext().getRealPath(
"images"); // 获得输入流
InputStream in = file.getInputStream();
// 定义文件输入流
OutputStream fileout = new FileOutputStream(dir + File.separator
+ newfilename);
int c = 0;
byte[] buffer = new byte[1024];
while ((c = in.read(buffer, 0, 1024)) != -1) { fileout.write(buffer, 0, c); }
file.destroy(); // 数据库记录信息
Product pro = new Product();
pro.setProductname(addForm.getProductname());
pro.setImagespath(imagespath);
biz.saveProduct(pro); } catch (Exception e) {
e.printStackTrace();
} return null;
}
新名称 时间方法:
DataDefine.java
public static String getDateId(){
String str=new String();
String month1=new String();
String day1=new String();
Calendar ca=Calendar.getInstance();
int year=ca.get(Calendar.YEAR);
str=str+year;
int month=ca.get(Calendar.MONTH)+1;
if(month<10)
month1="0"+month;
str=str+month1;
int day=ca.get(Calendar.DATE);
if(day<10)
day1="0"+day;
str=str+day1;
int hour=ca.get(Calendar.HOUR);
str=str+hour;
int minute=ca.get(Calendar.MINUTE);
str=str+minute;
int sec=ca.get(Calendar.SECOND);
int minsec=ca.get(Calendar.MILLISECOND);
str=str+sec+minsec;
return str;
}Jsp 相关代码:<html:form action="/add" enctype="multipart/form-data">
名称 : <html:text property="tname"/><html:errors property="productname"/><p/>
图片:<html:file property="file" /><p/>
<html:submit value="添加" />
</html:form>兄弟 够意思吧
希望对你的有用哦
^_*
DiskFileItemFactory dfif = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(dfif);
List list = sfu.parseRequest(request); byte by[] = (byte[])Utils.getFieldValue(list,"pic1");
if(by!=null){
java.io.OutputStream os = new java.io.FileOutputStream(request.getRealPath("uploadpic") +"/p" +dieId+ ".jpg");
os.write(by);
os.close();
}
http://topic.csdn.net/u/20090327/20/eb2554c2-8901-4212-ba4a-c97e2ab46ca9.html或者两个都是马甲?