简单使用fileupload上传图片

上传使用jar:

<dependency>    <groupId>commons-fileupload</groupId>    <artifactId>commons-fileupload</artifactId>    <version>1.3.1</version></dependency>

超链接传bookI

<a href="/jsp/manageuser/imgupload.jsp?bookId=${li.id}">上传封面</a>

页面取出bookId

String bookId = request.getParameter("bookId");

页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;    String bookId = request.getParameter("bookId");%>

<!DOCTYPE html>

<html lang="zh-CN"><head>    <meta charset="utf-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1">    <title>test</title>    <!-- Bootstrap -->    <script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>    <script src="/js/ajaxfileupload.js"></script></head>

<body><script type="text/javascript">    function up() {        $.ajaxFileUpload({            type: "POST",            url: ‘/manage/sendUp.action?bookId=<%=bookId%>‘,            secureuri: false,            fileElementId: ‘fileImg‘,//file标签的id            dataType: ‘text‘,//返回数据的类型            success: function (data, status) {                alert("成功");                window.location.href = "<%=basePath%>/manage/showBook.action";                //window.location.href = "<%=basePath%>/manage/toUpdateBook.action";            },            error: function () {                alert("异常");            }        });    }</script><input type="file" id="fileImg" name="fileImg"><input type="button" value="上传" onclick="up()"><input type="hidden" id="bookId" name="bookId" value="${id}"></body></html>controller:
//头像上传@RequestMapping(value = "/sendUp", method = RequestMethod.POST)@ResponseBodypublic String sendUp(@RequestParam("bookId") int bookId, @RequestParam MultipartFile[] fileImg, HttpServletRequest request, HttpServletResponse response) throws IOException {    Map<String, Object> map = new HashMap<>();   /* map.put("code", 1);    System.out.print("收到用户[" + bookId + "]的文件上传请求");*/    //文件实际上传路径    String realPath = request.getSession().getServletContext().getRealPath("/");    response.setContentType("text/plain;charset=UTF-8");    //设置响应给前台的PrintWriter对象  /*  PrintWriter out = response.getWriter();*/    String fileName = null;    for (MultipartFile myfile : fileImg) {        if (myfile.isEmpty()) {            /*out.print(map.toString());            out.flush();*/        } else {            try {                fileName = myfile.getOriginalFilename();                String trueFileName = String.valueOf(System.currentTimeMillis() + fileName);

String wlPath = "D:\\java\\xiangmu\\first_project\\web\\resources\\img\\" + trueFileName;                String xdPath = "../resources/img/" + trueFileName;                String tempPath = realPath + "resources\\img\\" + trueFileName;

map.put("bookId", bookId);                map.put("xdPath", xdPath);                isService.updateImg(map);

FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(wlPath));                FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(tempPath));               /* map.put("code", 0);*/             /*   out.print(map.toString());                out.flush();                int count = 1;                map.put("count", count);*/

} catch (IOException e) {              /*  e.printStackTrace();                out.print(map.toString());                out.flush();*/

}        }    }    return "1";}img文件夹位置

spring-mvc。xml:

设置上传文件大小<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    <property name="maxUploadSize" value="10485760"/></bean>

 自己用来做笔记的
时间: 2024-12-11 01:28:06

简单使用fileupload上传图片的相关文章

JavaScript实现简单的前端上传图片预览

JavaScript实现简单的前端上传图片预览 <div class="file_upload"> <div id="portrait"></div> <input type="file" name="" id="" onchange="showPerview(this)"> </div> <script type=&qu

C#中使用FileUpload上传图片到SQL数据库中以image类型存储并使用Image控件显示注意事项

当我们需要以数据流存储图片到数据库中(而不是文件路径),需要考虑很多因素,不同的环境决定了采取不同方法. 1.将图片存入数据库.首先,当我们决定使用FileUpload上传图片,需要考虑,FileUpload的功能主要是在客户端选取图片,然后使用FileUpload的SaveAs方法将选取的图片的地址保存到服务器端保存,因为我们使用的数据流存储图片,所以没有必要将图片保存到服务器端. 其次,FileUpload没有像OpenDialog的.Filter方法过滤图片的格式,即实现打开对话框,只能选

.Net 使用文件上传控件FileUpload上传图片

例1: 来源:http://long546324.iteye.com/blog/349946 Default.aspx文档: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1

angularJS file-up-load上传图片

<div class="form-group">                            <label class="col-xs-2 control-label col-xs-offset-1">系统图片</label>                            <div class="col-xs-7">                                 

简单的FileUpload文件上传

引入jar包:commons-fileupload-1.3.1.jar,commons-io-1.3.2.jar 前台HTML: <form action="./upload" method="post" enctype="multipart/form-data"> <table> <tr> <td><label>用户名:</label></td> <td&

FileUpload上传图片直接浏览显示(没有上传按钮如何上传)

1.给FileUpload添加一个onchange事件:FileUpload1.Attributes.Add("onchange", "document.getElementById('imgP').src=this.value"); 2.原来是单独的上传事件,现在没有上传按钮了,将上传的事件(代码)直接放入要执行的插入(提交信息)按钮事件中. protected void btnSubmit_Click(object sender, EventArgs e) { 

一个简单的struts2上传图片的例子

说到上传图片,最近在赶一个项目,也遇到了要上传头像的难题,在网上搜了好久,都没有找到满意的答案.于是又选择了最原始的上传图片的方法,在此做个记录,谨作备忘之用. 首先是添加页面add_UI.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <%@include file="/common/heade

fileupload上传图片

<asp:FileUpload ID="FileUpload1" runat="server" accept="image/*" /> HttpFileCollection upFiles = Request.Files; if (upFiles.Count > 0 && upFiles.Count < 2) { string wenjianjia = "a_" + DateTime.N

简单js fileUpload控件(单例)

single_file_upload.js var YsUIWidgets=YsUIWidgets||{}; YsUIWidgets.uploadFile = (function($){     var container  = null;     var changeCallback = function(){};     var acceptTypes = ["jpg","png"];// 接受的上传文件类型     var renderHtml = "