Flex版Mp3播放器----大部分功能版二,后台

三,java后台

      1.  文件上传,这里采用servlet的方式

        upload.java

        

package cn.jcracker.tingting;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.jcracker.tingting.bean.Song;
import cn.jcracker.tingting.service.SongService;

/**
 *
 * @author crystal
 */
public class Upload extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */



	private int maxPostSize = 100 * 1024 * 1024;

    public Upload() {
        super();
    }

    public void destroy() {
        super.destroy();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
System.out.println("进入后台上传:全文1---"+request.getInputStream());    

//得到容器中以实例化好的Bean
ApplicationContext ctx =  WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); 
SongService songService = (SongService)ctx.getBean("SongService"); 

String userName = request.getParameter("userName");  
userName=new String(userName.getBytes("ISO8859-1"),"utf-8");
String saySomethingToOne = request.getParameter("saySomethingToOne");  
saySomethingToOne=new String(saySomethingToOne.getBytes("ISO8859-1"),"utf-8");

		response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        
//保存文件到服务器中this.servlet.getServletContext().getRealPath("/UpLoad");
        String uploadPath = this.getServletContext().getRealPath("/upload/");
        String uuName = null;
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(4096);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(maxPostSize);
        try {
            List fileItems = upload.parseRequest(request);
            Iterator iter = fileItems.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    String name = item.getName();
                    //歌词不用保存到数据库
                    if(name.indexOf(".lrc")==-1){
                    	uuName = UUID.randomUUID().toString()+".mp3";
System.out.println("songName --------"+name);  
                    songService.SaveSong(uuName,name, userName, saySomethingToOne);
                    }else{
                    	//若是歌词,就按名称查出来,得到uuName
                    	String songName = name.substring(0, name.indexOf(".lrc", 0))+".mp3";
System.out.println("songName --------"+songName);                        	
                    	String tempUuName = ((Song)songService.getUUName(songName).get(0)).getUuName();
System.out.println("uuName --------"+tempUuName);                       	
                    	uuName = tempUuName.substring(0, tempUuName.indexOf(".mp3", 0))+".lrc";
System.out.println("uuName --------"+uuName);                    	
                    }
                    try {                   	
                        item.write(new File(uploadPath +"/"+ uuName));                       
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
            System.out.println(e.getMessage() + "结束");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>

}

 

  2. 其他类

    见附件,将项目和数据库上传了!Tomcat版的如果需要联系我,由于时间仓猝,来不及结尾  请有疑问的地方 Email联系  361276898@qq.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值